Django--url (template language {% url ' test1 ' param1=5 param2=6%})

Source: Internet
Author: User

If you want the form form to submit a URL that is similar to action= "/index-5-6.html", you can use the {% url ' test1 ' param1=5 param2=6%} in the HTML template language

urls.py

From django.conf.urls import URL, includefrom mytest import viewsurlpatterns = [    url R ' ^index-(? p<param1>\d+)-(? p<param2>\d+). html ', Views.index, name= ' test1 '),]

views.py

From django.http import httpresponsefrom django.shortcuts import renderfrom django.views import ViewDef index (req, param1 , param2):    return render (req, ' index.html ')

Html

<! DOCTYPE html>{% url ' test1 ' param1=5 param2=6%}" method= "POST" >        <input type= "text" name= "A"/>        <input type= "Submit" Name= "B" value= "Submit"/>    </form> </body>

Note:

This method can only return a dead URL, regardless of what is passed through the front-end access, the URL of the form form submission is fixed. As shown in the example above, the URL submitted is always action= "/index-5-6.html"

If you want to return the same URL as the Request.path_info effect, you can use reverse.

urls.py

From django.conf.urls import urlfrom mytest Import viewsurlpatterns = [    url R ' ^index-(? p<param1>\d+)-(? p<param2>\d+). html ', Views.index, name= ' test1 '),]

views.py

From django.http import httpresponsefrom django.shortcuts import renderdef index (req, param1, param2): from     Django.urls Import Reverse    reverse (' test1 ', kwargs={' param1 ': param1, ' param2 ': param2})    Return render (req, ' index.html ', {' URL1 ': X,})

Html

<! DOCTYPE html>{{URL1}}" method= "POST" >        <input type= " Text "name=" A "/>        <input type=" Submit "Name=" B "value=" Submit "/>    </form></body>

  

Django--url (template language {% url ' test1 ' param1=5 param2=6%})

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.