Django 2.1 python 3.7
Create a virtual environment
python -m venv virtual environment name
Enter the virtual environment
Download django
pip install django==2.1
1 Create a project: 2 3 Django-ADMIN startproject project name -> Create a project has a point 4 5 to create applications: 6 Python manage.py create applications startapp
A clean Django framework, we need to open the settings.py set
Step 1: Create a database: MyDB mysql command See related article: https://www.cnblogs.com/whatarey/p/11396616.html
Step two: settings.py background Django application settings Chinese, modify the configuration database to mysql
. 1 DATABASES = { 2 ' default ' : { . 3 ' ENGINE ' : ' django.db.backends.mysql ' , . 4 ' NAME ' : 'MyDB ' , # database name, . 5 ' the USER ' : ' the root ' , # database login username 6 ' pASSWORD ' : ' 123456 ' , # database password 7 'HOST': ' Localhost ' , # host database resides . 8 ' PORT ' : ' 3306 ' , # database port . 9 } 10 } . 11 12 is 13 is 14 LANGUAGE_CODE = ' ZH-Hans '
Step 3: Create the model:
1 class UserInfo (models.Model): 2 UserName = models.CharField (max_length = 20) # username 3 create_date = models.DateField () # Creation Date 4 pwd = models.CharField (max_length = 20) # password 5
The fourth step, the background admin.py registration
from .models import UserInfo admin.site.register(UserInfo)
The fifth step to perform file migration
python manage.py makemigrations
python manage.py migrate
At this time, certain that a mistake will occur:
Join in the project init.py in:
import pymysql pymysql.install_as_MySQLdb()
Running on it
run:
Into the background, suddenly found not to create background administrator account ....
Create an administrator account:
Python manage.py createsuperuser
This password is too pit. . . .
Step Six: Add to views.py project
1 from django.shortcuts import render 2 from django.http import HttpResponse,Http404 3 from django.template import loader 4 #这里是视图 5 6 def index(request): 7 '''写法1''' 8 # template=loader.get_template('Web_Templates/index.html') 9 # # 2.定义上下文 10 # context={"title":"Index","heatde_title":"Index","BodyHtml":"这是我的主页内容<a href=''>hehe</a>"} # 这个上下文是一个字典,它将模板内的变量映射为 Python 对象。 11 # return HttpResponse(template.render(context,request)) 12 13 '''写法2''' 14 data={ 15 "title":"Index", 16 "heatde_title":"Index", 17 "BodyHtml":"中间body", 18 "id":[1,2,3,4,5] 19 } 20 context = data 21 return render(request, 'Web_Templates/index.html', context) 22 # 注意到,我们不再需要导入 loader 和 HttpResponse 。 23 # 不过如果你还有其他函数(比如说 detail, results, 和 vote )需要用到它的话,就需要保持 HttpResponse 的导入。 24 25 26 def getHtml(request,id): 27 list=[0,1,3,4] 28 try: 29 question =list[id] 30 except Exception as e: 31 raise Http404("Question does not exist") 32 return render(request, 'Web_Templates/Show.html', {'question': question})
第七步:项目中添加urls.py
1 from django.urls import path 2 3 from . import views 4 5 app_name="Web" 6 urlpatterns=[ 7 #url(r'^$' 8 path("",views.index,name="Index"), 9 path("Show/<int:id>/",views.getHtml,name="Show"), 10 # ''' 11 # # # ex: /polls/ 12 # # path('', views.index, name='index'), 13 # # # ex: /polls/5/ 14 # # path('<int:question_id>/', views.detail, name='detail'), 15 # # # ex: /polls/5/results/ 16 # # path('<int:question_id>/results/', views.results, name='results'), 17 # # # ex: /polls/5/vote/ 18 # # path('<int:question_id>/vote/', views.vote, name='vote'), 19 # ''' 20 ]
第八步,应用的urls.py 注册
第九步:创建模板 我是在 MyWeb\
第十步,把模板路径添加到项目的settings.py
1 TEMPLATES = [ 2 { 3 'BACKEND': 'django.template.backends.django.DjangoTemplates', 4 'DIRS': [os.path.join(BASE_DIR, 'Templates')], # 模板'DIRS': [os.path.join(BASE_DIR, 'templates')], 5 'APP_DIRS': True, 6 'OPTIONS': { 7 'context_processors': [ 8 'django.template.context_processors.debug', 9 'django.template.context_processors.request', 10 'django.contrib.auth.context_processors.auth', 11 'django.contrib.messages.context_processors.messages', 12 ], 13 }, 14 }, 15 ]
index.html
1 <html> 2 <head> 3 <title>{{title}}</title> 4 </head> 5 <body> 6 <h1>{{heatde_title}}</h1> 7 <div> 8 {{BodyHtml}} 9 10 {% for a_link_id in id %} 11 12 <li><a href="{% url 'Web:Show' a_link_id %}" >点我有惊喜,我的编号是:{{a_link_id}}</a></li> 13 {% endfor %} 14 </div> 15 </body> 16 </html>
show.html
1 <html> 2 <head> 3 <title>111111</title> 4 </head> 5 <body> 6 <h1>{{question}}</h1> 7 <div> 8 {{question}} 9 10 </div> 11 </body> 12 </html>
可看官网:https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial03/
ok
当id 超过了list的最大值,就会404
喜欢就点个赞!!!文章写得真不容易。