开发者

URL Patterns in Django - Different Combinations

I'm finding it hard to understand what exactly is passed to the patterns method in Django.

You see, I usually have my urls.py as:

urlspatterns = patterns('example.views',

    (r'/$','func_to_call'),

)

Then in func_to_call I would get everything I want from the request object by using request.path. However on a second take, it's really quite horrific that I'm ignoring Django's slickness for such a longer, less clean way of parsing - the reason being I don't understand what to do!

Let's say you have 3 servers you're putting your Django application on, all of which have a domain name and some variation like server1/djangoApplicationName/queryparams, server2/application/djangoApplicationName and server3/queryparams. What will the ur开发者_运维技巧lpattern get passed? The whole url? Everything after the domain name?


The URLconf regex sees only the path portion of the URL, with the initial forward-slash stripped. Query parameters are not matched by the URLconf, you access those via request.GET in your view. So you might write a pattern like this:

urlpatterns = patterns('myapp.views',
    url(r'^myapp/something/$', 'something_view_func')
)

The documentation has more examples and details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜