How do I unit test django urls?

One way would be to reverse URL names and validate Example urlpatterns = [ url(r’^archive/(\d{4})/$’, archive, name=”archive”), url(r’^archive-summary/(\d{4})/$’, archive, name=”archive-summary”), ] Now, in the test from django.urls import reverse url = reverse(‘archive’, args=[1988]) assertEqual(url, ‘/archive/1988/’) url = reverse(‘archive-summary’, args=[1988]) assertEqual(url, ‘/archive-summary/1988/’) You are probably testing the views anyways. Now, to test that the URL connect … Read more

Django URLS, how to map root to app?

I know that this question was asked 2 years ago, but I’ve faced the same problem and found a solution: In the project urls.py: urlpatterns = patterns(”, url(r’^’, include(‘my_app.urls’)), #NOTE: without $ ) In my_app.urls.py: urlpatterns = patterns(”, url(r’^$’, ‘my_app.views.home’, name=”home”), url(r’^v1/$’, ‘my_app.views.v1′, name=”name_1″), url(r’^v2/$’, ‘my_app.views.v2′, name=”name_2″), url(r’^v3/$’, ‘my_app.views.v3’, name=”name_3″), )

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, … Read more

Django – after login, redirect user to his custom page –> mysite.com/username

A simpler approach relies on redirection from the page LOGIN_REDIRECT_URL. The key thing to realize is that the user information is automatically included in the request. Suppose: LOGIN_REDIRECT_URL = ‘/profiles/home’ and you have configured a urlpattern: (r’^profiles/home’, home), Then, all you need to write for the view home() is: from django.http import HttpResponseRedirect from django.urls … Read more

Django – is not a registered namespace

You should just change you action url in your template: <form action=”{% url ‘submit’ %} “method=’post’> On the note of url namespaces… In order to be able to call urls using home namespace you should have in your main urls.py file line something like: for django 1.x: url(r’^’, include(‘home.urls’, namespace=”home”)), for django 2.x and 3.x … Read more

Django 2.0 path error ?: (2_0.W001) has a route that contains ‘(?P

The new path() syntax in Django 2.0 does not use regular expressions. You want something like: path(‘<int:album_id>/’, views.detail, name=”detail”), If you want to use a regular expression, you can use re_path(). re_path(r’^(?P<album_id>[0-9])/$’, views.detail, name=”detail”), The old url() still works and is now an alias to re_path, but it is likely to be deprecated in future. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)