Create and set up simple Django project
Create virtual environment and working directories for Django.
Let’s make a directory.
mkdir Project
Go there
cd Project
Installing a virtual environment
python -m venv .venv
Now, activate virtual environment.
source ./.venv/bin/activate
source ./.venv/Scripts/activate
To check a successful activated virtual environment, execute this command to be sure:
pip list
We will see either 0 or a couple of them. Or we can run a next command on the Linux system.
which python
And if the displayed path to Python is in our working directory, i.e. Project/.venv so everything is working.
How to create Django project
Install a package called Django.
pip install django
Start a new project called Project1
django-admin startproject Project1
Go to Project1
cd Project1
Accept all migrations.
./manage.py migrate
How to start Django project / testing it
Now you can be able to start start testing(local) server.
./manage.py runserver
If everything is done right, you shall see:
0
Used termins
- Django migrations ⟶ Are a way of propagating changes made to your Django models (which represent your database schema) into the actual database schema. They are a core part of Django's ORM (Object-Relational Mapping) system and help you manage the database schema in a versioned and structured manner.
- Django framework ⟶ Is a high-level, open-source web framework for building web applications using the Python programming language. It follows the model-view-template (MVT) architectural pattern and is designed to facilitate rapid development while promoting clean, pragmatic design.
- Python programming language ⟶ It is interpreted, build upon object-oriented approach, with dynamic semantics, and yes it s high-level programming language. Actively using for rapid development, scripting and gluing part of existing apps.
Related questions
- I can’t stand Django template language. Do I have to use it? I think this template engine is the best thing ever, but I know that choosing a template language runs close to religion. There’s nothing about Django that requires using the template language, so if you’re attached to Jinja2, Mako, or whatever it's ok.
- How can I see the raw SQL queries Django is running? Make sure your Django DEBUG setting is set to True. Then import connection from django.db. connection.queries is only available if DEBUG is True. It’s a list of dictionaries in order of query execution. Each dictionary has the sql and time property.
- If I make changes to a model, how do I update the database? Take a look at Django’s support for schema migrations. If you don’t mind clearing data, your project’s manage.py utility has a flush option to reset the database to the state it was in immediately after migrate was executed.