Reprinted from: http://www.runoob.com/django/django-first-app.html
Reference: https://www.cnblogs.com/feixuelove1009/p/5823135.html
Django creates the first project
In this chapter, we will introduce the Django management tool and how to use Django to create a project. For the first project, we will command the project with HelloWorld.
Test release notes:
-
Python 2.7.10
-
Django 1.10.6
Django admin tool
After installing Django, you should now have the admin tool django-admin.py available. We can use django-admin.py to create a project:
We can take a look at the command introduction of django-admin.py:
[root@solar ~]# django-admin.py Usage: django-admin.py subcommand [options][args]Options:-v VERBOSITY,--verbosity=VERBOSITY Verbosity level;0=minimal output,1=normal output,2=verbose output,3=very verbose output --settings=SETTINGS ThePython path to a settings module, e.g."myproject.settings.main".Ifthis isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used. --pythonpath=PYTHONPATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Raise on exception --version show program's version number andexit-h,--help show this help message andexitType'django-admin.py help <subcommand>'for help on a specific subcommand.Available subcommands:[django] check cleanup compilemessages createcachetable ... omitted part...
Create your first project
Use django-admin.py to create the HelloWorld project:
django-admin.py startproject HelloWorld
*The command entered in which folder directory is created in which folder
After the creation is complete, we can view the directory structure of the following project:
$ cd HelloWorld/ $ tree .|--HelloWorld||-- __init__.py ||-- settings.py ||-- urls.py |`-- wsgi.py `-- manage.py
Directory Description:
- HelloWorld: The container for the project.
- manage.py: A useful command-line tool that lets you interact with this Django project in various ways.
- HelloWorld/__init__.py: An empty file that tells Python that this directory is a Python package.
- HelloWorld/settings.py: Settings/configuration for this Django project.
- HelloWorld/urls.py: The URL declaration for this Django project; a "directory" of websites powered by Django.
- HelloWorld/wsgi.py: The entry point for a WSGI compatible web server to run your project.
Next we enter the HelloWorld directory and enter the following command to start the server:
python 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.
*In pycharm, it is possible to:
Enter the ip and port number of your server in the browser. If it starts normally, the output is as follows:
View and URL Configuration
Create a new view.py file in the HelloWorld directory under the previously created HelloWorld directory and enter the code:
HelloWorld/HelloWorld/view.py file code:
Next, bind the URL to the view function. Open the urls.py file, delete the original code, and copy and paste the following code into the urls.py file:
HelloWorld/HelloWorld/urls.py file code:
from . import view
urlpatterns = [
url ( r ' ^$ ' , view . hello ) ,
]
整个目录结构如下:
$ tree .|--HelloWorld||-- __init__.py ||-- __init__.pyc ||-- settings.py ||-- settings.pyc ||-- urls.py # url 配置||-- urls.pyc ||-- view.py # 添加的视图文件||-- view.pyc # 编译后的视图文件||-- wsgi.py |`-- wsgi.pyc `-- manage.py
完成后,启动 Django 开发服务器,并在浏览器访问打开浏览器并访问:
我们也可以修改以下规则:
HelloWorld/HelloWorld/urls.py 文件代码:
from . import view
urlpatterns = [
url ( r ' ^hello$ ' , view . hello ) ,
]
通过浏览器打开 http://127.0.0.1:8000/hello,输出结果如下:
注意:项目中如果代码有改动,服务器会自动监测代码的改动并自动重新载入,所以如果你已经启动了服务器则不需手动重启。
url() 函数
Django url() 可以接收四个参数,分别是两个必选参数:regex、view 和两个可选参数:kwargs、name,接下来详细介绍这四个参数。
-
regex: 正则表达式,与之匹配的 URL 会执行对应的第二个参数 view。
-
view: 用于执行与正则表达式匹配的 URL 请求。
-
kwargs: 视图使用的字典类型的参数。
-
name: 用来反向获取 URL。