Saturday, February 20, 2021

Attempt to reproduce "Django 3 Tutorial & CRUD Example with MySQL and Bootstrap" on Fedora 33 KVM Guest

Regardless I've spent some time to overcome several issues (typos) in Original Howto

https://www.techiediaries.com/django/django-3-tutorial-and-crud-example-with-mysql-and-bootstrap/

I strongly believe that mentioned article is one of the best for beginners to test the taste of Django 3.1.16

To accomplish the task  following issues have to be fixed :-

The core fix in views.py  :-
 
def edit(request, pk, template_name='crudapp/edit.html'):
    contact = get_object_or_404(Contact, pk=pk)
    form = ContactForm(instance=contact)
    if request.method == 'POST':
        form = ContactForm(request.POST, instance=contact)
    # form = ContactForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        return redirect('index')
    return render(request, template_name, {'form':form})
 
Original code provided
def edit(request, pk, template_name='crudapp/edit.html'):
    contact = get_object_or_404(Contact, pk=pk)
    form = ContactForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        return redirect('index')
    return render(request, template_name, {'form':form})
The code above doesn't allow edit records in mariadb database ( OS Linux Fedora 33 Server ) 

Another fix add import to urls.py 

from django.contrib import admin 
from django.urls import path 
from crudapp import views

************************
Update as of 02/24/2021
************************

Overall files layout
 
(.env) [boris@fedora33srv djangoCrudExample]$ ls -CRl
.:
total 4
drwxr-xr-x. 6 boris boris 187 Feb 20 17:58 crudapp
drwxr-xr-x. 3 boris boris 108 Feb 20 18:09 djangoCrudExample
-rwxr-xr-x. 1 boris boris 673 Feb 18 21:16 manage.py
./crudapp:
total 24
-rw-r--r--. 1 boris boris   63 Feb 18 21:26 admin.py
-rw-r--r--. 1 boris boris   89 Feb 18 21:26 apps.py
-rw-r--r--. 1 boris boris  157 Feb 18 21:32 forms.py
-rw-r--r--. 1 boris boris    0 Feb 18 21:26 __init__.py
drwxr-xr-x. 3 boris boris   67 Feb 18 21:30 migrations
-rw-r--r--. 1 boris boris  604 Feb 18 21:30 models.py
drwxr-xr-x. 2 boris boris  150 Feb 20 17:34 __pycache__
drwxr-xr-x. 3 boris boris   21 Feb 18 22:19 templates
-rw-r--r--. 1 boris boris   60 Feb 18 21:26 tests.py
-rw-r--r--. 1 boris boris 1470 Feb 20 17:34 views.py
./crudapp/migrations:
total 4
-rw-r--r--. 1 boris boris 1043 Feb 18 21:30 0001_initial.py
-rw-r--r--. 1 boris boris    0 Feb 18 21:26 __init__.py
drwxr-xr-x. 2 boris boris   72 Feb 18 21:31 __pycache__
./crudapp/migrations/__pycache__:
total 8
-rw-r--r--. 1 boris boris 982 Feb 18 21:31 0001_initial.cpython-39.pyc
-rw-r--r--. 1 boris boris 151 Feb 18 21:30 __init__.cpython-39.pyc
./crudapp/__pycache__:
total 20
-rw-r--r--. 1 boris boris  181 Feb 18 21:30 admin.cpython-39.pyc
-rw-r--r--. 1 boris boris  569 Feb 18 22:03 forms.cpython-39.pyc
-rw-r--r--. 1 boris boris  140 Feb 18 21:30 __init__.cpython-39.pyc
-rw-r--r--. 1 boris boris  801 Feb 18 21:30 models.cpython-39.pyc
-rw-r--r--. 1 boris boris 1834 Feb 20 17:34 views.cpython-39.pyc
 
./crudapp/templates:
total 0
drwxr-xr-x. 2 boris boris 104 Feb 19 00:15 crudapp
./crudapp/templates/crudapp:
total 20
-rw-r--r--. 1 boris boris  895 Feb 18 21:43 base.html
-rw-r--r--. 1 boris boris  749 Feb 18 21:46 confirm_delete.html
-rw-r--r--. 1 boris boris 2653 Feb 18 21:45 create.html
-rw-r--r--. 1 boris boris 2455 Feb 18 21:46 edit.html
-rw-r--r--. 1 boris boris 2157 Feb 18 21:44 index.html
 
./djangoCrudExample:
total 16
-rw-r--r--. 1 boris boris  411 Feb 18 21:16 asgi.py
-rw-r--r--. 1 boris boris    0 Feb 18 21:16 __init__.py
drwxr-xr-x. 2 boris boris  122 Feb 19 00:38 __pycache__
-rw-r--r--. 1 boris boris 3271 Feb 18 21:52 settings.py
-rw-r--r--. 1 boris boris 1120 Feb 19 00:34 urls.py
-rw-r--r--. 1 boris boris  411 Feb 18 21:16 wsgi.py
./djangoCrudExample/__pycache__:
total 16
-rw-r--r--. 1 boris boris  150 Feb 18 21:21 __init__.cpython-39.pyc
-rw-r--r--. 1 boris boris 2391 Feb 18 21:52 settings.cpython-39.pyc
-rw-r--r--. 1 boris boris 1262 Feb 19 00:38 urls.cpython-39.pyc
-rw-r--r--. 1 boris boris  573 Feb 18 22:03 wsgi.cpython-39.pyc

Server Console


























































































Database status verification










Initial setup on Server F33 already running mariadb-server :-

$ python3 -m venv .env

$ source .env/bin/activate

$ sudo dnf install mariadb-connector-c-devel
$ sudo dnf groupinstall "C Development Tools and Libraries"
$ sudo dnf install python-devel
$ pip3 install mariadb
$ pip install mysqlclient

No comments:

Post a Comment