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修改

通过路由传参

posted @ 2020-12-29 21:50  reblue520  阅读(161)  评论(0编辑  收藏  举报