1,通过路由反向解析进行重定向
1.1 添加视图函数
myshop/app2/views.py
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import reverse
def index(request):
return HttpResponse("app2 的index")
# 反向解析
def url_reverse(request):
# 使用reverse()方法反向解析
print('解析结果:'+ reverse("app2_url_reverse")) # 使用reverse()方法反向解析 ,reverse("app2_url_reverse") 对应urls的 name='app2_url_reverse'
return render(request, "2/url_reverse.html")
def test_get(request):
print(request.get_host()) # 域名加端口
print(request.build_absolute_uri()) # 获取完整的请求URI,包括查询字符串
print(request.path) # 获取访问路径,不含参数
print(request.get_full_path()) # 获取访问路径,不含参数
print(request.method) # 获取请求中使用的HTTP方式(POST/GET)
print(request.GET) # 获取GET请求的参数
print(request.META["HTTP_USER_AGENT"]) # 用户浏览器的user-agent字符串
print(request.META["REMOTE_ADDR"]) # 客户端IP地址
print(request.GET.get('username')) # 获取get参数
return HttpResponse("")
# 获取请求中的使用HTTP方式(POST/GET)
def test_post(request):
print(request.method)
print(request.POST.get('username'))
return render(request, '2/test_post.html')
def test_response(request):
response = HttpResponse()
response.write("<h1>hello django</h1>")
response.write("<br>")
response.write(response.content)
response.write("<br>")
response.write(response['Content-Type'])
response.write("<br>")
response.write(response.status_code)
response.write("<br>")
response.write(response.charset)
response.write("<br>")
return response
def test_render(request):
return render(request, '2/test_render.html', {'info':'hello django'}, content_type='text/html; charset=utf-8')
from django.shortcuts import redirect
def test_redirect_views(request):
return redirect('test_render')
1.2 添加路由地址
myshop/app2/views.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
path('url_reverse', views.url_reverse, name='app2_url_reverse'), # 使用reverse()方法反向解析 ,name对于视图的reverse("app2_url_reverse")
path('test_get', views.test_get, name='test_get'),
path('test_post', views.test_post, name='test_post'),
path('test_response', views.test_response, name='test_response'),
path('test_render', views.test_render, name='test_render'),
path('test_redirect_views', views.test_redirect_views, name='test_redirect_views'),
]
1.3 访问页面
http://127.0.0.1:8000/app2/test_redirect_views
重定向至http://127.0.0.1:8000/app2/test_render
2,通过绝对的或相对的URL,让浏览器跳转到指定的URL进行重定方向
2.1 添加视图函数
myshop/app2/views.py
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import reverse
def index(request):
return HttpResponse("app2 的index")
# 反向解析
def url_reverse(request):
# 使用reverse()方法反向解析
print('解析结果:'+ reverse("app2_url_reverse")) # 使用reverse()方法反向解析 ,reverse("app2_url_reverse") 对应urls的 name='app2_url_reverse'
return render(request, "2/url_reverse.html")
def test_get(request):
print(request.get_host()) # 域名加端口
print(request.build_absolute_uri()) # 获取完整的请求URI,包括查询字符串
print(request.path) # 获取访问路径,不含参数
print(request.get_full_path()) # 获取访问路径,不含参数
print(request.method) # 获取请求中使用的HTTP方式(POST/GET)
print(request.GET) # 获取GET请求的参数
print(request.META["HTTP_USER_AGENT"]) # 用户浏览器的user-agent字符串
print(request.META["REMOTE_ADDR"]) # 客户端IP地址
print(request.GET.get('username')) # 获取get参数
return HttpResponse("")
# 获取请求中的使用HTTP方式(POST/GET)
def test_post(request):
print(request.method)
print(request.POST.get('username'))
return render(request, '2/test_post.html')
def test_response(request):
response = HttpResponse()
response.write("<h1>hello django</h1>")
response.write("<br>")
response.write(response.content)
response.write("<br>")
response.write(response['Content-Type'])
response.write("<br>")
response.write(response.status_code)
response.write("<br>")
response.write(response.charset)
response.write("<br>")
return response
def test_render(request):
return render(request, '2/test_render.html', {'info':'hello django'}, content_type='text/html; charset=utf-8')
from django.shortcuts import redirect
def test_redirect_views(request):
return redirect('test_render')
def test_redirect_url(request):
return redirect('http://www.baidu.com')
2.2 添加路由地址
myshop/app2/urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
path('url_reverse', views.url_reverse, name='app2_url_reverse'), # 使用reverse()方法反向解析 ,name对于视图的reverse("app2_url_reverse")
path('test_get', views.test_get, name='test_get'),
path('test_post', views.test_post, name='test_post'),
path('test_response', views.test_response, name='test_response'),
path('test_render', views.test_render, name='test_render'),
path('test_redirect_views', views.test_redirect_views, name='test_redirect_views'),
path('test_redirect_url', views.test_redirect_url, name='test_redirect_url'),
]