Let’s start right away, open the visual studio code and create a new app, which we call blog, with the following command:
python manage.py startapp blog
Once this is done, let’s immediately create a simple model in the file models.py.
Let’s apply the migrations to create the sqlite database with the following commands:
python.exe manage.py makemigrations
python.exe manage.py migrate
I create the forms.py file and edit it like this:
As you can see from the image above, this Form inherits from ModelForm. This class does nothing but specify the model and all the fields of the model. Let’s now modify views.py to manage user interactions.
We create the folder templates inside our app and inside it a new folder that we call blog. Inside the blog folder, I create the html template that I called createpost.html. Let’s go to the settings.py file, register the application and the template.
Let’s record the path in the file urls.py.
We create a superuser to be able to access the Django Admin panel and check the data entered via Form. To improve the graphic aspect, install the following package with the command pip install crispy-bootstrap5 Installed the package, go again to the settings.py file and configure it. I underline in red the three lines to be modified.
Once the package is configured, using it is very simple within the template; in fact, we import the package and next to form we make a pipe with the following text crispy. Let’s run the development server, maybe first create a link to the blog on the homepage and try to create the posts. Let’s check everything with the Django Admin panel.
Leave A Comment