by Mohd Shibli
Posted on 25 January 2017
In the previous tutorial, we learned how to Install Django and create a new project in it and learned about the different files present in the project folder.
Previous tutorial: How to Build Simple Blogging System using Django-Part-1
In this tutorial, we will learn about Apps, Models, how to create models and how to connect our web application with the database.
A Django app basically encapsulates different functionality modules such as a blog, forum, gallery etc. You can create Django app by executing the following command. before executing the command you first need to locate the project folder on the terminal in my case it was the desktop.
python manage.py startapp blogapp
You can give your app some other name but in this case, it is "blogapp".
After executing the above command you will see a folder named blogapp created in your project folder containing some files as shown below
Now let's see what each file in the app directory actually means.
admin.py: The code that sets up the admin interface fro our website
models.py: Database models for our website
tests.py: Here you can write unit tests for your application
views.py: The views contain code that renders the actual content that will appear in a user's browser.
A Django model is the python equivalent of a table in a database. A database table generally holds information about a single type of data, and Django allows us to configure the structure of this table using python code.
To create models we simply need to create a python class and place it inside the models.py file located in the app directory, in this case, it will be blogapp/models.py.
So without wasting more time lets create a class name Article in the file models.py(BlogSystem/blogapp/models.py) the code for the class is shown below(please type the code yourself instead of doing copy and paste)
from django.db import models # Create your models here. class User(models.Model): author_name = models.CharField(max_length=30) author_description = models.TextField() def __str__(self): return self.author_name class Article(models.Model): title = models.CharField(max_length=250) slug = models.SlugField(max_length=255) author = models.ForeignKey(User) content = models.TextField() creation_date = models.DateTimeField(auto_now_add=True) featured_image = models.ImageField(null=True, blank=True) def __str__(self): return self.title
The above code creates an Artice model with:
You also notice a function __str__(self) which is returning the title of the article. If we omit this then Django does not know how to give this model a name, and thus by default uses names that are unhelpful.
Now we have successfully created our model now we will learn how to activate these models or in other terms, we can say we are going to create the database fro the defined models.
The first step is to open the following file BlogSystem/blogapps/apps.py and copy the class name present in the file, see below
Now after copying the name type in the following code in setting.py file present in the following directory BlogSystem/BlogSystem/settings.py
If you have a different class name then do not worry it can differ just follow the above steps.
Now paste it as shown below
Now open your terminal and execute the following commands
python manage.py makemigrations blogapp python manage.py migrate
Why we do this is because we wanted to let Django know that we have created Models in our "BlogApp" and Activated them.
Now we have successfully activated our models, But we are not able to perform actions on them so to do so we will activate our admin panel and in our admin panel we will be able to see all the models that we have created plus we will be able to perform different operations such as insert, delete, update etc on them.
Go to the admin.py file in the "blogapps" folder and type in the below code in it
from django.contrib import admin from .models import Article, User # Register your models here. admin.site.register(Article) admin.site.register(User)
This code will tell Django to show the models Article and User in the admin panel interface, if you will not follow this step then Django will not show the created models in the admin panel.
Now it'stime to test, go to your terminal and locate to your project directory(in my case it is the desktop) and execute the following command.
python manage.py createsuperuser
After executing the command you have to enter some details such as username and password(while typing the password you will not be able to see it), this command basically creates a user for the admin panel.
Now we need to run the Django server to go to the admin panel for that type in the following command in your terminal before that locate to the project folder
python manage.py runserver
Now open your web browser and go to the following URL http://localhost:8000/admin, now admin panel will get open and will look something like this
And after logging in you will see the models created by you are appearing in our admin panel and you can add, delete and modify values so go ahead and try it.
So that's it, in the next tutorial we will learn how to create views and will learn about URL routeing, so stay tuned and keep on coding.
Thank you for reading.
Also, don't forget to subscribe to our Newsletter.
If you like this article, then please share it and help us grow.
Preorder and Postorder Traversal of binary tree in Python
02 September 2018
Binary Tree in Python
02 September 2018
Explaining Register variables in C with examples
17 August 2018
Data Autosave System using PHP, MySQL and AJAX
06 July 2018