1. 程式人生 > 實用技巧 >django傳參常用的方法

django傳參常用的方法

訊息釋出與接收示例

(python37_django2) D:\python>django-admin startproject message_test

(python37_django2) D:\python>cd message_test

# 建立app

(python37_django2) D:\python\message_test>python manage.py startapp app

在settings.py中新增app

檢視的建立 app/views.py

方法1:通過get瀏覽器位址列獲取引數

app路由urls

# _*_ coding:utf-8 _*_
# __author__ == 'jack' # __date__ == '2020-12-29 8:27 PM' from django.urls import path from .views import LessionTwo urlpatterns = [ path('two/ ', LessionTwo.as_view(), name="two") ]

檢視app/views.py

# coding:utf-8

from django.views.generic import View
from django.http import HttpResponse


class
LessionTwo(View): def get(self, request): message = request.GET.get('message', 'no contains') return HttpResponse(message)

在總的urls中引入app的urls

from django.contrib import admin
from django.urls import path,include
from app import urls as app_urls

urlpatterns = [
    path('admin/
', admin.site.urls), path('', include(app_urls)) ]

方法2:

通過resful風格獲取引數

App/views修改

通過路由傳參