Django Creating the First Project
In this chapter, we will introduce the Django management tools and how to use Django to create a project. The first project will be named HelloWorld.
Test Version Information:
- Python 3.7.4
- Django 3.0.6
Django Management Tools
After installing Django, you should now have the available management tool django-admin
. If Windows does not have the environment variables configured, you can use django-admin
.
Let's take a look at the command introduction for django-admin:
$ django-admin
Type 'django-admin 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
……omitted part……
Creating the First Project
Use django-admin to create the HelloWorld project:
django-admin startproject HelloWorld
After creation, we can view the project's directory structure:
$ cd HelloWorld/
$ tree
.
|-- HelloWorld
| |-- __init__.py
| |-- asgi.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py
Directory Explanation:
- HelloWorld: The container for the project.
- manage.py: A practical command-line tool that allows you to interact with this Django project in various ways.
- HelloWorld/_init_.py: An empty file that tells Python that this directory should be considered a Python package.
- HelloWorld/asgi.py: An entry point for ASGI-compatible web servers to serve your project.
- HelloWorld/settings.py: The settings/configuration of this Django project.
- HelloWorld/urls.py: The URL declarations for this Django project; a "directory" of the website driven by Django.
- HelloWorld/wsgi.py: An entry point for WSGI-compatible web servers to serve your project.
Next, we enter the HelloWorld directory and enter the following command to start the server:
python3 manage.py runserver 0.0.0.0:8000
0.0.0.0 allows other computers to connect to the development server, and 8000 is the port number. If not specified, the port number defaults to 8000.
Enter your server's IP address (here we enter the local IP address: 127.0.0.1:8000) and the port number in the browser. If it starts normally, the output will be as follows:
Views and URL Configuration
In the HelloWorld directory under the previously created HelloWorld directory, create a new views.py file and enter the following code:
HelloWorld/HelloWorld/views.py File Code:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
Next, bind the URL to the view function. Open the urls.py file, delete the existing code, and copy and paste the following code into the urls.py file:
HelloWorld/HelloWorld/urls.py File Code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.hello),
]
The entire directory structure is as follows:
$ tree
.
|-- HelloWorld
| |-- __init__.py
|-- __init__.pyc
|-- settings.py
|-- settings.pyc
|-- urls.py # URL configuration
|-- urls.pyc
|-- views.py # Added view file
|-- views.pyc # Compiled view file
|-- wsgi.py
|-- wsgi.pyc
`-- manage.py
After completing the setup, start the Django development server and access it in the browser at:
We can also modify the following rules:
## HelloWorld/HelloWorld/urls.py File Code:
from django.urls import path
from . import views
urlpatterns = [ path('hello/', views.hello), ]
Open **http://127.0.0.1:8000/hello** in your browser, and the output will be:
**Note:** If there are any code changes in the project, the server will automatically detect the changes and reload them, so there is no need to manually restart the server if it is already running.
---
## path() Function
Django's path() function can accept four parameters: two required parameters, route and view, and two optional parameters, kwargs and name.
path(route, view, kwargs=None, name=None)
- route: A string representing the URL rule. URLs matching this rule will execute the corresponding view in the second parameter.
- view: Executes the URL request that matches the regular expression.
- kwargs: Dictionary-type parameters used by the view.
- name: Used to reverse retrieve the URL.
In Django 2.0, you can use the re_path() method to be compatible with the **url()** method from version 1.x. Some regular expression rules can also be implemented using re_path().
from django.urls import include, re_path
urlpatterns = [ re_path(r'^index/$', views.index, name='index'), re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'), re_path(r'^weblog/', include('blog.urls')), ... ]