Ref: 79229784
Django is an open-source Web application framework written by Python.
Install Django:pip install Django
Python and Django version correspondence table
Django version |
Python versions |
1.8 |
2.7, 3.2 (until the end of 2016), 3.3, 3.4, 3.5 |
1.9, 1.10 |
2.7, 3.4, 3.5 |
1.11 |
2.7, 3.4, 3.5, 3.6 |
2.0 |
3.5+ |
1. Create a Django project (MyWeb)
Django-admin startproject myweb
2. Start the Django service
Python manage. PY Runserver
Browser input: Http://127.0.0.1:8000/will be able to access the ~
3. Create a Django APP
What is a Django app, in Django, the app is the equivalent of a function module, and other web frameworks may be very different from other features in different apps, convenient code reuse.
Python manage. py Startapp MyApp
You can now access: http://127.0.0.1:8000/admin
1), create a folder in the MyApp directory templates
, and create a index.html file in that folder to display the content of the Web page.
The contents of the index.html file are as follows:
<! DOCTYPE html><html lang= "en" > <head> < meta charset= "UTF-8" > <title>hello,django! </title> </head> <body> <h1> Hello,my First django! </h1> </ body> </html>
2), add new APP under myweb/myweb/settings.py, find Installed_app, add ' MyApp, ' and then Save:
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘myapp‘, ]
3), open the views.py file under the MyApp folder and enter it in the file:
from django.shortcuts import render# Create your views here.# 添加index函数,用于返回index.html页面def index(request): return render(request, ‘index.html‘)
4), open the MyWeb folder under the urls.py file, enter in the file:
from django.contrib import adminfrom django.urls import pathfrom myapp import views # 导入viewsurlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘‘, views.index), # 添加views.index]
5), complete the above steps, execute "Python manage.py runserver" start the server in Django, open in the browser 127.0.0.1:8000
Summary:
Python Code
1 2 3 4 5 |
|
django-admin.py Startproject MyWeb#创建项目 python manage.py startapp MyApp#创建app python manage.py runserver#启动Django中的开发服务器 python manage.py-h#帮助文档 python manage.py <command> [options]#Django命令 |
Python+django (Python Web project First experience)