Hi, guys!đ¤ Hope youâre doing well. I want to show you how to make a website with Django! Hope you will enjoy it!đ
Prerequisites:
- Basic knowledge of Python đ¤
- Understanding what is HTML, CSS, JavaScriptđ¤
Okay, let’s get startedđ¤Š. First of all, we have to establish an environment for our project and install Django. Open your favorite IDE (Integrated development environment) or command prompt with an empty folder. I opened it in VS Code.đ
You can download it here https://code.visualstudio.com/
Put this command python -m venv env in your terminal. It will make a virtual environment which is called env. đ
Next, letâs activate the environment by this command env/Scripts/activate on Windows, try source venv/bin/activate on Mac, if it doesnât work just look it up on the Internet for your OS. If it is activated you will see (env) on the left of the prompt.đ§âđť
Install Django using this command: pip install djangođ¨âđź
Then create a new project using this command: django-admin startproject my_project ., I called my project my_project. Point in the end adds our project to our folder without adding a new folder with the same name.
We have a lot of files, most of them we are not going to use in this tutorial. đľđľđľ
Setting.py Itâs our main file where we can change some configuration of our project,
Urls.py Here we will add path to our apps.
Manage.py We will be using it during migrations and adding new apps.đ˛
Letâs add a new app python manage.py startapp main đ¤:
We have a new app called main.
Letâs add a new file urls.py in the main app and import path from Django.urls, from . (point means import from this app ) import views, write urlpatterns = []đ¤Ż:
Add your app path in my_project urls, import include from Django.urls as well:
Then in my_project settings.py add app name (main) into INSTALLED_APPS like thisđ:
Make migration using this command: python manage.py migrateđŽâđ¨
This command saves everything in the database (SQLite), you can see the new file db.sqlite3:
Letâs create a superuser by using this command python manage.py createsuperuser, add username, email, and password đŽââď¸:
We can run our project python manage.py runserver and open the admin page, just click on http://127.0.0.1:8000/ + CTRL, if you want to stop CTRL+CđŠâđť:
And put in your URL http://127.0.0.1:8000/admin, then put your name and password to log in admin pageđŠâđť:
You will see Django administration page:
You can change the appearance by changing the configuration in admin CSS file(not in this tutorial).
Add in models.py new modelđ Almost done:
And make migrations python manage.py makemigration and python manage.py migrate:
Add model in admin.py đ¨âđ¨:
After this, we can see it on our admin page:
We can add data into categoryđ¨âđ¨:
We have PC has blank text and it works because we added blank=True before in our model.đ¨âđ
Create templates folder in main app and then inside this folder add a folder named main.
Add index.html file into this main folder.
Add this in views.py in main app:
Categories take our data from the database and return render shows on the screen.đ¤¨
In main urls.py add path with imported views.
Add this to index.html fileđŠâđ¨:
block endblock â means start and end of dynamic part of the index.html
for endfor â itâs like for loop in Python.
category.title â itâs our data from database.đ§
On localhost page you will see our datađ:
It works!!! It was the first part of the tutorial, in the next parts I will show how to add CSS, JavaScript and how delete and add data etc.đĽłđĽłđĽł Thank you for reading. I hope you enjoyed it!đĽłđĽłđĽł
Let’s connect on X(Twitter) https://twitter.com/volodys1ove
Discord: https://discord.gg/z8Dzb4Hv