Django
| Introduction | |
| Install Django | |
| Commands | |
| Add url | |
| Related Articles |
Introduction
- Install Python if it is not there.
- Decide what you need virtual environment or not
- Install Django
Installing Django on your host is quite simple - you can study the instructions below.
To
install Django on virtual hosting
It will take more manipulation. How to do it on the hosting
Beget
you can find out from my article
«Install Django on virtual hosting»
Many modern
hosting
The procedure will be similar.
Installation Django
The first thing I recommend is to update the pip.
python3 -m pip install --upgrade pip
Then install Django directly.
python3 -m pip install django
Check the installation
python3 -m django --version
3.2.4
or
python3 -m pip list
Package Version ----------------- -------- asgiref 3.3.4 Django 3.2.4 pip 21.1.2 pytz 2021.1 setuptools 39.0.1 sqlparse 0.4.1 typing-extensions 3.10.0.0
Django Teams
The list of Django commands can be obtained by performing
python3 -m django help
Type 'python -m django help <subcommand>' for help on a specific subcommand. Available subcommands: [django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserver Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
Each project usually uses one or more applications (application).
When all applications and configurations are combined, they make up a web application.
applications + configurations = web application
Create the first app.
python3 manage.py startapp first_app
A first app directory will appear with the following files:
admin.py
apps.py
__init__.py
migrations
models.py
tests.py
views.py
Go to Project Name/Project Name
Edit Project Name/Project Name/settings.py
You need to add to the list of installed applications our new app first app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first_app',
]
Restarting the server
python3 manage.py runserver
If the restart is carried out using a script, we perform (As far as I know, this is true for beget and dreamhost hosting)
(docker) andreyolegovich@server:~/eth1.ru [0] $ touch tmp/restart.txt
Create a view and link to the URL
The process consists of at least three actions.
- In a directory with an application, for example Project_Name/first_app file views.py You need to create a function that will listen to the URL.
- The main directory of the project, for example Project_Name/Project_Name file urls.py You need to import this function.
- Still in urls.py You need to add a combination of url and functions in urlpatterns
Example:
Go to the directory with Project Name/first app and edit
views.py
To create our first view.
Each view must return some HttpResponse object.
Add the following code to the file Project_Name/first_app/views.py:
from django.http import HttpResponse
def about(request):
return HttpResponse("About Us!")
Now you need to link this view to some URL
Add the following code to the file
urls.py
from first_app.views import about
urlpatterns = [
path('', 'about')
]
In urlpatterns, you can specify the url in different ways
urlpatterns = [ path('', welcome), # Main page page path('welcome.html', welcome), #url with .html extension path('admin/', admin.site.urls), #url with a slash at the end path('date', date) #url without extension ]
Pay attention to welcome – one feature can serve multiple URLs
Add the domain on which you deploy the site in the ALLOWED HOSTS file (from the project) settings.py
ALLOWED_HOSTS = ['http://www.eth1.ru','127.0.0.1','localhost','eth1.ru','www.eth1.ru']
| Project_Name | |||
|---|---|---|---|
| Project_Name | first_app | templates | |
|
__init.py__ __pycache__ settings.py connection to first_app by INSTALLED_APPS = ['first_app', ] connection to templates by TEMPLATES_DIR = os.path.join( BASE_DIR, "templates") urls.py wsgi.py |
admin.py apps.py __init.py__ migrations models.py __pycache__ test.py urls.py views.py | first_app | … |
Useful references
documentation
tutorial
overview
Article author: Andrei Olegovich
| DJANGO | |
| Installation on hosting | |
| How to choose hosting | |
| Errors | |
| Video Tutorials | |
| Python | |
| HTML | |
| CSS | |
| Bash | |
| Docker | |
| Frameworks |