Questions tagged [django-urls]

The Django URL dispatch system

The Django URL dispatch system, which allows to bind a view to a URL pattern. This mechanism allows to get clean URL scheme, without any extensions, an idea supported by Tim Berners-Lee.

When Django can’t find a regex matching the requested URL, or when an exception is raised, Django will invoke an error-handling view.

Example

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^articles/2003/$', 'news.views.special_case_2003'),
    url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
    url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
    url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)

See: https://docs.djangoproject.com/en/stable/topics/http/urls/

3758 questions
227
votes
10 answers

Getting Django admin url for an object

Before Django 1.0 there was an easy way to get the admin url of an object, and I had written a small filter that I'd use like this: ... Basically I was using the url reverse function with the view name…
hasen
  • 161,647
  • 65
  • 194
  • 231
226
votes
21 answers

How can I list urlpatterns (endpoints) on Django?

How can I see the current urlpatterns that "reverse" is looking in? I'm calling reverse in a view with an argument that I think should work, but doesn't. Any way I can check what's there and why my pattern isn't?
interstar
  • 26,048
  • 36
  • 112
  • 180
225
votes
7 answers

Django optional URL parameters

I have a Django URL like this: url( r'^project_config/(?P\w+)/(?P\w+)/$', 'tool.views.ProjectConfig', name='project_config' ), views.py: def ProjectConfig(request, product, project_id=None,…
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
178
votes
6 answers

Is it better to use path() or url() in urls.py for django 2.0?

In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I've seen other examples on youtube of this. e.g. from django.contrib import admin from django.urls import…
FrostedCookies
  • 2,253
  • 2
  • 13
  • 15
169
votes
6 answers

What is a NoReverseMatch error, and how do I fix it?

I have some code and when it executes, it throws a NoReverseMatch, saying: NoReverseMatch at /my_url/ Reverse for 'my_url_name' with arguments '()' and keyword arguments '{}' not found. n pattern(s) tried: [] What does this mean, and what can I do…
Sayse
  • 42,633
  • 14
  • 77
  • 146
163
votes
5 answers

STATIC_ROOT vs STATIC_URL in Django

I am confused by static root and want to clarify things. To serve static files in Django, the following should be in settings.py and urls.py: import os PROJECT_DIR=os.path.dirname(__file__) 1. Absolute path to the directory in which static files…
user993563
  • 18,601
  • 10
  • 42
  • 55
157
votes
5 answers

ImportError: cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0

After upgrading to Django 4.0, I get the following error when running python manage.py runserver ... File "/path/to/myproject/myproject/urls.py", line 16, in from django.conf.urls import url ImportError: cannot import name 'url'…
Alasdair
  • 298,606
  • 55
  • 578
  • 516
157
votes
7 answers

ImproperlyConfiguredError about app_name when using namespace in include()

I am currently trying out Django. I use the namespace argument in one of my include()s in urls.py. When I run the server and try to browse, I get this error. File…
Nelson M
  • 1,658
  • 4
  • 13
  • 19
127
votes
5 answers

Django URL Redirect

How can I redirect traffic that doesn't match any of my other URLs back to the home page? urls.py: urlpatterns = patterns('', url(r'^$', 'macmonster.views.home'), #url(r'^macmon_home$', 'macmonster.views.home'), url(r'^macmon_output/$',…
felix001
  • 15,341
  • 32
  • 94
  • 121
123
votes
6 answers

Django URLs TypeError: view must be a callable or a list/tuple in the case of include()

After upgrading to Django 1.10, I get the error: TypeError: view must be a callable or a list/tuple in the case of include(). My urls.py is as follows: from django.conf.urls import include, url urlpatterns = [ url(r'^$', 'myapp.views.home'), …
Alasdair
  • 298,606
  • 55
  • 578
  • 516
106
votes
8 answers

django urls without a trailing slash do not redirect

I've got two applications located on two separate computers. On computer A, in the urls.py file I have a line like the following: (r'^cast/$', 'mySite.simulate.views.cast') And that url will work for both mySite.com/cast/ and mySite.com/cast. But…
whatWhat
  • 3,987
  • 7
  • 37
  • 44
104
votes
9 answers

How to get the current url name using Django?

I have to build an url dynamically according to the current url. Using the {% url %} tag is the easiest way to do it, but I need the current url name to generate the new one dynamically. How can I get the url name attached to the urlconf that leads…
Bite code
  • 578,959
  • 113
  • 301
  • 329
102
votes
7 answers

Using {% url ??? %} in django templates

I have looked a lot on google for answers of how to use the 'url' tag in templates only to find many responses saying 'You just insert it into your template and point it at the view you want the url for'. Well no joy for me :( I have tried every…
Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88
91
votes
3 answers

How to handle request.GET with multiple variables for the same parameter in Django

In a Django view you can access the request.GET['variablename'], so in your view you can do something like this: myvar = request.GET['myvar'] The actual request.GET['myvar'] object type is: Now, if you want to pass…
ismail
  • 3,882
  • 5
  • 36
  • 47
89
votes
7 answers

Django 2.0 path error ?: (2_0.W001) has a route that contains '(?P<', begins with a '^', or ends with a '$'

I'm trying to create the back-end code for a music application on my website. I have created the correct view in my views.py file (in the correct directory) as shown below: def detail(request, album_id): return HttpResponse("

Details for…

Joe Tynan
  • 895
  • 1
  • 6
  • 5
1
2 3
99 100