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

Django learning about URLs configuration

編輯:Python

First step : Create project :

Enter at the command line :django-admin startproject myweb

Go to the folder where the project is located cd myweb

Command line input :python manage.py runserver     ( remarks : Run development services )

The second step : Create application :

Command line input :python manage.py startapp myapp

The third step : Write the first view

1. In the project myweb Of settings.py In the document INSTALLED_APPS Add the name of the application , The code is as follows :

INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'myapp'

]

 2.  In the application myapp Create a new urls.py

urls.py Add inside views The path of , The code is as follows :

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index),     # Go here and call testapp Of views.py file
    
]

3. And then myapp In the app views.py Write the code as follows :

from django.http import HttpResponse # This is a Django Add one http Response function of

def index(request):    
    return HttpResponse("Hello, world.")  

4. In the project myweb Of url.py Add an application to the file myapp Of urls, The code is as follows :

from django.contrib import admin

from django.urls import path,include

urlpatterns = [

    path('admin/', admin.site.urls),

    path('ok/',include('myapp.urls'))    #ok The path is named casually , Then the browser accesses ok Just go

]

5. Enter at the command line :python manage.py runserver 

A browser access address will appear

 6. Type in the browser : You can see that hello,world

  When writing views, you must follow this order !


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