Development environment to build
Python environment installed
Download: https: //www.python.org/downloads//
Django installation
Open Windows CMD inputpip install django
PyCharm IDE (Community Edition) Installation
Download: https: //www.jetbrains.com/pycharm/download/#section=windows
Creating the HelloWorld project
By Django's startproject
creation project in the directory command
The following figure is through startproject
project files created by the command structure
- manage.py project management documents
- settings.py project profiles
- urls.py project management document routing
- wsgi.py WSGI is an interface related information
More about creating a project and a description of each file: https: //docs.djangoproject.com/en/2.2/intro/tutorial01/
Project Once created, via terminal commands runserver
directly to start
#如果有类似异常,可能是django没有安装好,在终端中再重装一次django包(pip install django)
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
After the file system detects no problems, will start the service
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 23, 2019 - 15:57:10
Django version 2.2.2, using settings 'hello_world.settings'
Starting development server at http://127.0.0.1:8000/
Then open the browser http://127.0.0.1:8000/
, the page can be opened normally, indicating that everything is normal
By Django of startapp
creating an application package in the project command
In the shop package, each file described
- views.py view processing
- tests.py test
- models.py defined application model
- apps.py statement Application
- Admin module management objects defined admin.py
Add a new view in the project
In views.py file, add hello () function
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hello(request):
return HttpResponse("Hello World")
After the increase in view of the function, we also need to configure the routing, and the view URL binding, or browse on the web page can not be added to the view that we
Routing Configuration
First, create a file in the shop urls.py routing package ,, This is an application-level routing configuration
from django.urls import path
import shop.views
urlpatterns = [
path('hello', shop.views.hello)
]
然后还需要对项目层次的路由进行配置,打开hello_world包下的urls.py文件,然后增加path('shop/', include('shop.urls'))
,如果请求地址中含有shop
就转发到刚才配置的shop.urls
路由文件中
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
]
项目主程序中添加应用
打开settings.py
文件,然后进行引用配置(这里配置的方法ShopConfig
是在创建应用的时候自动生成的Apps.py
文件中的一个方法)
现在所有的配置我们已经完成,可以通过runserver
命令启动服务,打开浏览器输入http://127.0.0.1:8000/shop/hello
,到此我们一个基本的HelloWorld项目已经算是创建完成