BigZ
BigZ

Reputation: 886

Django parameter not in querydict but in view

i'm facing some strange behavior in Django 1.8.3. (Note: I started the project with 1.7.4) I want to reuse a view in case it gets an get-request, so i defined following urls in the urls.py located at the same place where my settings.py is.

(Note: this is the short version)

urlpatterns = patterns('',
    url(r'^licenses/$', views.licenses),
    url(r'^licenses/(?P<pool>.*)/$', views.licenses, name='pool'),
)

in my views.py i defined following view:

def licenses(request, pool=None):
    print request, request.GET
    print pool

the template (base.html) calling the view:

<li><a href="/licenses/">Licenses</a>
    <ul>
        {% for pool in LICENSE_MENU %}
            <li class="dir"><a href="/licenses/{{ pool }}">{{ pool }}</a></li>
        {% endfor %}
    </ul> 
</li>

LICENSE_MENU is a list of licenses passed by a context_processor to the base.html

Everything works almost fine. If i click a license i get redirected to licenses() but the license i clicked doesn't show up in the request. The output of my shell when i run the testserver and click on the link is following:

[06/Oct/2015 12:16:03]"GET / HTTP/1.1" 200 129165
<WSGIRequest: GET '/licenses/'> <QueryDict: {}>
[06/Oct/2015 12:16:06]"GET /licenses/ HTTP/1.1" 200 128597
<WSGIRequest: GET '/licenses/ansys/'> <QueryDict: {}>
ansys
[06/Oct/2015 12:16:11]"GET /licenses/ansys/ HTTP/1.1" 200 128851

Why does my view print pool although it is not in the request querydict? What am i missing? Thanks in advance.

Upvotes: 1

Views: 149

Answers (2)

Alasdair
Alasdair

Reputation: 309049

The pool isn't part of the get parameters, it is extracted from the url and passed to the view as the variable pool. That's why print pool works in your view.

If your url was

/licenses/?pool=ansys

then the url /licences/ would match the regex r'^licenses/$' in your url patterns, and pool would be in the get parameters.

Upvotes: 4

hsfzxjy
hsfzxjy

Reputation: 1322

Actually, the URL parameters that are behind a ? will be extracted and put into the GET. You can get pool from the view function's arguments.

Upvotes: 2

Related Questions