[Django]url Parameters/reverse and Httpresponseredirect

Source: Internet
Author: User

Reference

You need to complete the following tasks
- 访问http://127.0.0.1:8000/                  返回"hello maotai"或home.html- 访问http://127.0.0.1:8000/add/?a=1&b=2      返回之和- 访问http://127.0.0.1:8000/add2/2/3          返回值和- 访问http://127.0.0.1:8000/add2_v2/2/3       跳转到add2,并返回之和
Project Code Implementation

views.html Writing
views.html#### 返回字符串def index(request):    return HttpResponse("hello maotai")#### url参数 ?a=1&b=2 方式传参def add(request):    a = request.GET['a']    b = request.GET['b']    c = int(a) + int(b)    return HttpResponse(c)#### url参数/2/3 方式传递def add2(request, a, b):    c = int(a) + int(b)    return HttpResponse(str(c))#### 重定向到add2, url的cname#### 模板解析也用到了reverse函数,解析谁? 解析html,将解析到的结果返回def add2_v2(request, a, b):    return HttpResponseRedirect(reverse('add2', args=(a, b)))#### 返回htmldef home(request):    return render(request, "home.html")
urls.py
urlpatterns = [    path('', views.index),    path('home/', views.home),    path('add/', views.add),    path('add2/<int:a>/<int:b>', views.add2, name='add2'),    path('add2_v2/<int:a>/<int:b>', views.add2_v2, name='add2_v2'),    path('admin/', admin.site.urls),]
Home.html
<!DOCTYPE html>

[Django]url Parameters/reverse and Httpresponseredirect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.