[Answer]-Django url parse formatted url

1👍

You’re confusing between sending information through your urls with GET and formatting you urls with arguments for the view functions. Say I am visiting a site called http://www.mysite.com/ and the page has a form that looks like this:

<form>
<input type='text' name='category' id='category'></input>
<button type='submit'>Send!</button>
</form>

upon clicking, the url will automatically change to http://www.mysite.com/?category=<value of input>. The ? marks that everything afterwards should be treated as GET data, with the syntax of <id>=<value>. You can then access them like so:

def response(request):
    category = request.GET['category']

formatting urls is different, because it means looking for patterns that are part of the url. i.e. a pattern that looks like r'^/(?P<category>\d+)/$' will look for this: http://www.mysite.com/<category>/ and it will send it to the request in your views as an additional argument like so:

def response(request, category):
    ...

The regex is used to define how you recognize that part of the url. For example, the \d+ you’re using means that category needs to be a number. You can search how to define different types of patterns according to your needs

Note that with GET you are sending the data to the same view function that rendered the page you are currently visiting, while using a different url means you tell it where to go through your urls.py (usually a different function). Does that make things a bit clearer?

👤yuvi

Leave a comment