62👍
Instead of importing the logout_view
function, you should provide a string in your urls.py
file:
So not (r'^login/', login_view),
but (r'^login/', 'login.views.login_view'),
That is the standard way of doing things. Then you can access the URL in your templates using:
{% url login.views.login_view %}
118👍
The selected answer is out of date and no others worked for me (Django 1.6 and [apparantly] no registered namespace.)
For Django 1.5 and later (from the docs)
Warning
Don’t forget to put quotes around the function path or pattern name!
With a named URL you could do:
(r'^login/', login_view, name='login'),
...
<a href="{% url 'login' %}">logout</a>
Just as easy if the view takes another parameter
def login(request, extra_param):
...
<a href="{% url 'login' 'some_string_containing_relevant_data' %}">login</a>
- [Django]-Django query get last n records
- [Django]-How exactly do Django content types work?
- [Django]-Printing Objects in Django
51👍
Make sure (django 1.5 and beyond) that you put the url name in quotes, and if your url takes parameters they should be outside of the quotes (I spent hours figuring out this mistake!).
{% url 'namespace:view_name' arg1=value1 arg2=value2 as the_url %}
<a href="{{ the_url }}"> link_name </a>
- [Django]-Django select only rows with duplicate field values
- [Django]-Django templates: verbose version of a choice
- [Django]-How to do math in a Django template?
18👍
The url
template tag will pass the parameter as a string and not as a function reference to reverse()
. The simplest way to get this working is adding a name
to the view:
url(r'^/logout/' , logout_view, name='logout_view')
- [Django]-How to do math in a Django template?
- [Django]-Django fix Admin plural
- [Django]-How to pass multiple values for a single URL parameter?
12👍
I run into same problem.
What I found from documentation, we should use namedspace.
in your case {% url login:login_view %}
- [Django]-Django CSRF Cookie Not Set
- [Django]-How to use regex in django query
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
1👍
Judging from your example, shouldn’t it be {% url myproject.login.views.login_view %}
and end of story? (replace myproject
with your actual project name)
- [Django]-Cancel an already executing task with Celery?
- [Django]-Error when using django.template
- [Django]-How to update an existing Conda environment with a .yml file
1👍
For example, there are 4 views in my_app1/views.py
as shown below. *You can see the doc explaining URL namespaces in detail:
# "my_app1/views.py"
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def test1(request):
return HttpResponse("Test1")
def test2(request):
return HttpResponse("Test2")
def test3(request):
return HttpResponse("Test3")
And, there are 2 paths in my_app1/urls.py
as shown below:
# "my_app1/urls.py"
from django.urls import path
from . import views
app_name = "my_app1"
urlpatterns = [
path('test1/', views.test1, name="test1"),
path('test2/', views.test2, name="test2"),
]
And, there are 4 paths in core/urls.py
as shown below:
# "core/urls.py"
from django.contrib import admin
from django.urls import path, include
from my_app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('my_app1/', include('my_app1.urls')),
path('test3/', views.test3, name='test3'),
]
Now, you can set these URL namespaces to url tag in index.html
as shown below:
{% "index.html" %}
<a href="{% url 'admin:index' %}">Admin</a>
<a href="{% url 'my_app1:test1' %}">Test1</a>
<a href="{% url 'my_app1:test2' %}">Test2</a>
<a href="{% url 'test3' %}">Test3</a>
- [Django]-Get user profile in django
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-What is the best django model field to use to represent a US dollar amount?