程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Create the first Django framework program with Python

編輯:Python

Catalog

One . environment variable

Two . establish Django Framework Program

3、 ... and . Console

Four . Realization Django Application

5、 ... and . Start project

6、 ... and . Summary

One . environment variable

Right click on my computer –>> attribute –>> Advanced system setup –>> senior –>> environment variable –>>

Set up PATH attribute :

stay PATH Attribute Python Of Python.exe The installation directory and Scripts Catalog :

After setting these two properties , open pycharm, You can use it freely terminal Console. .

Two . establish Django Framework Program

In turn, click :File–>>new Project–>>Django–>> The second one in the following figure is selected by default –>>Create

3、 ... and . Console

pip list : Output the package name of the current project
pip install Django==3.1.7 : The installation version is 3.1.7 Of Django package , Version does not write the default installation maximum version
pip uninstall Django : Delete... In the current project Django package
python manage.py startapp stu : Create subapplication

Four . Realization Django Application

Django Under the default initial application project urls.py:

from django.contrib import adminfrom django.urls import path, includeurlpatterns = [ path('admin/', admin.site.urls),# default , Don't worry about this path('stu/', include('stu.urls')),# This is the sub application you created , Use include Skip to... Under sub application urls.py in ]

Django Under the default initial application project settings.py:
Add the name of the created sub application to this :

# Application definitionINSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'stu'# The sub application I created ]

Sub application of urls.py:

Will initialize... In the application urls.py Copy and paste into the created sub application directory

from stu import viewsurlpatterns = [ path('home/', views.home),# Name to jump to , And the functions called by the view path('goHtml/', views.goHtml)]

Sub application of views.py:

from django.http import HttpResponsefrom django.shortcuts import render# Create your views here.# Function call execution returns HttpResponse The contents of the page def home(request): return HttpResponse('<a href="#" rel="external nofollow" rel="external nofollow" >hello Django</a>')# The function call execution returns a rendered html page def goHtml(request): return render(request, 'index.html')

establish html file :

stay temlates Create under file html file

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <a href="#" rel="external nofollow" rel="external nofollow" >hello python</a></body></html> 5、 ... and . Start project

Click the button shown in the figure below :

After entering, the default is :

Add just... After the navigation bar urls We set the name attribute :

6、 ... and . Summary

This chapter outlines the use of Python Create the first one Django Framework applications , The whole is relatively simple , At present, I am only familiar with the use of the framework , As long as the page can be output, there is no problem .

This is about using Python Create the first one Django This is the end of the framework program article , More about Python establish Django Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved