DEV Community

Volodyslav
Volodyslav

Posted on • Updated on

How to start with Django

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:

  1. Basic knowledge of Python 🤓
  2. 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/

vs code
Put this command python -m venv env in your terminal. It will make a virtual environment which is called env. 🖖
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.🧑‍💻
activation
Install Django using this command: pip install django👨‍💼
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.

project

files
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 🤔:
app
We have a new app called main.

app files
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 = []🤯:

urls
Add your app path in my_project urls, import include from Django.urls as well:

main urls
Then in my_project settings.py add app name (main) into INSTALLED_APPS like this🙃:

settings
Make migration using this command: python manage.py migrate😮‍💨

migrate

success migration
This command saves everything in the database (SQLite), you can see the new file db.sqlite3:

sqlite3
Let’s create a superuser by using this command python manage.py createsuperuser, add username, email, and password 👮‍♂️:

superuser
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👩‍💻:

start
And put in your URL http://127.0.0.1:8000/admin, then put your name and password to log in admin page👩‍💻:

url

admin
You will see Django administration page:

admin 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:

model
And make migrations python manage.py makemigration and python manage.py migrate:

makemigrations

migrate
Add model in admin.py 👨‍🎨:

admin
After this, we can see it on our admin page:

admin
We can add data into category👨‍🎨:

data

data
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.

templates
Add index.html file into this main folder.

index

Add this in views.py in main app:
Categories take our data from the database and return render shows on the screen.🤨

views
In main urls.py add path with imported views.

urls
Add this to index.html file👩‍🎨:

index

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😎:

page

Thank you for reading. I hope you enjoyed it!🥳🥳🥳
Let's connect on X(Twitter) https://twitter.com/volodys1ove
Discord: https://discord.gg/z8Dzb4Hv

Top comments (0)