Determine Complete Django Url Configuration

Determine complete Django url configuration

Django is Python, so introspection is your friend.

In the shell, import urls. By looping through urls.urlpatterns, and drilling down through as many layers of included url configurations as possible, you can build the complete url configuration.

import urls
urls.urlpatterns

The list urls.urlpatterns contains RegexURLPattern and RegexURLResolver objects.

For a RegexURLPattern object p you can display the regular expression with

p.regex.pattern

For a RegexURLResolver object q, which represents an included url configuration, you can display the first part of the regular expression with

q.regex.pattern

Then use

q.url_patterns

which will return a further list of RegexURLResolver and RegexURLPattern objects.

How can I get the full/absolute URL (with domain) in Django?

Use handy request.build_absolute_uri() method on request, pass it the relative url and it'll give you full one.

By default, the absolute URL for request.get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.

>>> request.build_absolute_uri()
'https://example.com/music/bands/the_beatles/?print=true'
>>> request.build_absolute_uri('/bands/?print=true')
'https://example.com/bands/?print=true'

Django URL configuration

It should probably look like something like that:

urlpatterns = patterns('',
(r'^(?P<city>[a-z-]+)/(?P<area>[a-z-]+)/$', 'yourapp.views.areaview'),
(r'^(?P<city>[a-z-]+)/(?P<area>[a-z-]+)/(?P<entry>[a-z-]+)/$', 'yourapp.views.entryview'),
)

How to get URL of current page, including parameters, in a template?

Write a custom context processor. e.g.

def get_current_path(request):
return {
'current_path': request.get_full_path()
}

add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS settings variable, and use it in your template like so:

{{ current_path }}

If you want to have the full request object in every request, you can use the built-in django.core.context_processors.request context processor, and then use {{ request.get_full_path }} in your template.

See:

  • Custom Context Processors
  • HTTPRequest's get_full_path() method.

Django URL configuration

urls.py

url(r'^(?P<item>[-\w]+)/purchase/$', 'purchase_view', name='purchase_view'),
url(r'^(?P<item>[-\w]+)/purchase/(?P<gift>gift)/$', 'purchase_view', name='gift_view'),

views.py

def purchase_view(request, item, gift=False):
if gift:
form = GiftForm
else:
form = PurchaseForm
...

How to get the current URL within a Django template?

Django 1.9 and above:

## template
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters

Old:

## settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)

## views.py
from django.template import *

def home(request):
return render_to_response('home.html', {}, context_instance=RequestContext(request))

## template
{{ request.path }}

Django: runtime url configuration

There is a contrib app in Django that already does this, it is called FlatPages. It works by registering a middleware. When a page is requested if it is not found it throws a 404 which is caught by the middleware. The middleware looks up the page in the database if found it serves the page and if not it throws a 404.

How to configure urls.py in django to redirect to another file's urls?

Simply do:

path('notes/', include('notes.urls'))


Related Topics



Leave a reply



Submit