In this blog, we will create a blog website in Django framework with full features.
Prerequisites
- Python Installation
If Python is not installed in your system, you can go to below link to download and install. https://www.python.org/downloads
2. PIP Installation
3. Git Installation
4. Xampp Installation
5. Visual studio code IDE
This blog has multiple parts:
- Creating a git repository and Django project — Part 1
- Migrate SQLite database to MySQL database — Part 2
- Create a Django application and model — Part 3
- Create a view to display the list of articles — Part 4
- Create a view to display a article details — Part 5
- Creating SEO-friendly URLs for articles — Part 6
- Add pagination to the article list view — Part 7
- Creating a comment system to the article — Part 8
- Adding the tagging functionality to the article — Part 9
- Getting similar articles — Part 10
- Display the latest articles in the sidebar of the blog website — Part 11
- Display the most commented articles in the sidebar of the blog website — Part 12
- Adding a sitemap to the blog site — Part 13
- Implement search functionality to the blog website — Part 14
Let us start to create a blog website in django.
Create a repository in GitHub
This step is not necessary, if you don’t want to use this project for version control. I want to use the blog app for version control, so I’m creating a repository in GitHub.
Created an empty GitHub repository. We have to keep a copy of the repository URL; we need that when we clone the repository. Now we have to clone this repository on my local system.
Clone the repository in local system
Go to your project directory where you want to keep your project. Here my project directory is E:\django-projects. You can choose according to your choice. After that type below command and press enter key:
git clone "YOUR_GITHUB_REPOSITORY_URL"
git clone https://github.com/nutanbhogendrasharma/blog_app.git
Now blog_app repository cloned in my local system. We can go to project directory and see the cloned repository.
Create a virtual environment
Go to project directory and create a virtual environment
I have cloned my repository in E:\django-projects, so open command prompt and go to that directory. After that, create a virtual environment. In this example, my virtual environment name is my_blog_env. You can keep virtual environment name according to your choice.
cd E:\django-projects\blog_app
python -m venv my_blog_env
Activate the virtual environment
I am working on windows machine, so type command for activation:
YOUR_VIRTUAL_ENV_NAME\Scripts\activate
my_blog_env\Scripts\activate
We can see virtual environment is activated now.
Install Django in activated environment
Next, we have to install Django in activated virtual environment. For that type following command:
pip install Django
Create a Django project in activated environment
django-admin startproject name [directory]
Creates a Django project directory structure for the given project name in the current directory or the given destination.
By default, the new directory contains manage.py and a project package (containing a settings.py and other files).
If only the project name is given, both the project directory and project package will be named and the project directory will be created in the current working directory.
If the optional destination is provided, Django will use that existing directory as the project directory, and create manage.py and the project package within it. Use ‘.’ to denote the current working directory.
In this example, I am creating a Django project called my_blog in . (current directory).
django-admin startproject my_blog .
After running above command, my_blog folder/directory created in current directory and manage.py file also.
These files are:
manage.py: A command-line utility that lets you interact with this Django project in various ways. We can read all the details about manage.py in django-admin and manage.py.
The inner my_blog/ directory is the actual Python package for our project. Its name is the Python package name, we need to use to import anything inside it.
my_blog/__init__.py: An empty file that tells Python that this directory should be considered a Python package.
my_blog/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
my_blog/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site.
my_blog/asgi.py: An entry-point for ASGI-compatible web servers to serve our project.
my_blog/wsgi.py: An entry-point for WSGI-compatible web servers to serve our project.
Migrate initial database
python manage.py makemigrations
python manage.py migrate
Run the development server
python manage.py runserver
Open a browser firebox or chrome and type http://127.0.0.1:8000/, you should get welcome page like below:
Create a super user
Stop the development server and create a super user to administration site. To stop the development server, press Ctrl + C together.
python manage.py createsuperuser
My super user details are:
name: nutan
password: xxxxxx
Now super user created, let us run again development server and see the admin part.
Run the development server and view the admin section
python manage.py runserver
The development server is running, to view admin section type below url:
After login, next screen should see like below:
Stop the development server and dectivate the virtual environment.
To stop the development server press Ctrl + c. To deactivate the virtual environment type “deactivate” in command prompt.
Push latest files to git repository
Create a .gitignore file in root folder
Add .gitignore file in root folder. We have to add the folder/file name in .gitignore file which we don’t want to push in github repository. We should not push virtual environment folder, so adding in that.
Open .gitingore file and include my_blog_env/ folder in that.
Save and close the file.
Push files to the github repository
Then type below commands:
git add --all
git commit -m "virtual environment and django peoject created"
git push origin main
When first time we push the files to the github repository, it will ask for authentication like below:
Sign in to the github account.
After successful authentication, you need to close the tab.
We can see in the command prompt files pushed to the main branch.
We can also see latest files pushed in github account.
That’s it in this part. In next part, we will migrate SQLite to MySQL database. If you have any query related to my blog, you can email me at nutanbhogendrasharma@gmail.com.
Thanks for reading 😊😊😊.