NoReverseMatch in Django 1.10 - python

Here is my url.py
from django.conf.urls import url
from django.contrib import admin
from app import views, auth
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name = 'index'),
url(r'^login/', auth.login, name = 'login'),
url(r'^logout/', auth.logout, name = 'logout'),
]
When I'm using in template <li>Administration</li> get error
NoReverseMatch at /
Reverse for 'admin' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
So can any one tell me how to solve this? Thank you very much.

Use admin:index if you want to have an url to /admin/ site.
If you install django-extensions you can use ./manage.py show_urls to get the list of urls for your app

Set admin url in your project urls.py, same folder as your settings.py
Url(r'^admin/', admin.site.urls),
Then call it in your template:
<a href="{% url 'admin:index' %} > link </a>

You should use admin namespace, like written in the docs. You could also look on other admin urls in that namespace.
{% url 'admin:index' %}

Related

how to format a a url to return something like this "product/4/improved-led-lamp" in the template

I'm trying to return a formatted url that will look like this "edit-book/4/improved-led-lamp" using the {% url %} tag
I've tried {% url 'dashboard:editbook' 4 slug %}
here is the root url.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('home.urls')),
path('dashboard/', include('dashboard.urls')),
path('admin/', admin.site.urls),
]
here is my dashboard app's urls,py
from django.urls import path
from . import views
app_name = 'dashboard'
urlpatterns = [
path('edit-book/<int:id>/<slug:title>', views.BookView.as_view(), name='editbook'),
]
if I visit "edit-book/4/improved-led-lamp" in my browser, it resolves, but when I try to reproduce this in the view using {% url %} tag, it throws noReverseMatch error
this is the error
screenshot of error
Your syntax looks correct and if slug variable consists of slug ([-a-zA-Z0-9_]+) it should be working.
Look into contents of slug. Error screenshot shows that it's an empty string.

Template for reusable Django app is not rendering

I have a separate app, and I'd like to render its template.
urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^guestbook/', include('guestbook.urls', namespace='guestbook', app_name='guestbook'))
]
guestbook/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
guestbook/views.py
def index(request):
entries = Entry.objects.all().order_by('-date')
return render(request, 'guestbook/index.html', {'entries': entries})
templates/guestbook/index.html
{% extends 'guestbook/base.html' %}
{% block content %}
Comment</span>
{% endblock %}
But I'm getting error:
NoReverseMatch at /guestbook/
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found.
0 pattern(s) tried: []
Error during template rendering
In template /Users/bulrathi/Yandex.Disk.localized/Learning/Code/Test tasks/myproject/templates/guestbook/index.html, error at line 27
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
27 <span class="meta-chunk">Комментировать</span>
I'll be so grateful for advice.
The problem was in base.html template. There were such hrefs as Выйти and Войти. Changing them to Выйти and Войти solved the problem.

No Reverse Match with Django Auth Views

I created a custom User model following the example in the Django documentation, now I'm trying to use Django auth views but I keep getting NoReverseMatch at /accounts/login/
Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This is the url conf:
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html',
'authentication_form': LoginForm}),
url(r'^logout/$', auth_views.logout, {'next_page': '/accounts/login'}),
url(r'^$', home, name="home"),
]
And I have this line in my template:
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% url 'django.contrib.auth.views.login' %}
This is incorrect. We put the name given to the url here instead of the location of the view.
Please see https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url. You need to provide a name for login like you have for home and then use that.
Correct way is:
urls.py ->
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html','authentication_form': LoginForm}, name="login") ,
template ->
<form method="post" action="{% url 'login' %}">

Django NoReverseMatch error, 0 patterns tried

I've tried about a thousand different fixes suggested on different questions about this, but nothing seems to be working.
urls.py (app):
from django.contrib.auth.decorators import login_required
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', login_required(views.index), name='index'),
url(r'^clock/', views.clock, name='clock'),
]
urls.py (project):
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from mqtt.views import auth, acl, superuser
urlpatterns = [
url('^accounts/login/', auth_views.login,
{'template_name': 'login.html'}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^auth$', auth),
url(r'^superuser$', superuser),
url(r'^acl$', acl),
url(r'^$', include('lamp.urls')),
]
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render
def index(request):
lamps = request.user.lamp_set.all()
context = {}
if lamps:
device_id = lamps[0].model
context = {'device_id': device_id}
return render(request, 'index.html', context)
def clock(request):
return render(request, 'clock.html')
index.html (snippet):
<div class="toggle-button" id="power"></div>
<div class="toggle-button" id="alarm-clock">
<a href="{% url 'clock' %}" id='clock-link'>Clock</a>
</div>
When I try to load the page I get:
NoReverseMatch at /
Reverse for 'clock' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
I'm pretty new to Django, so any help would be greatly appreciated.
$ means the end of urlconf search for django. you need to remove it to enable the inner-app urls search.
urlpatterns = [
url(r'^', include('lamp.urls')), # <----
#... rest of the urlconfs
]

Django NoReverseMatch

I'm making a simple login app in django 1.6 (and python 2.7) and I get an error at the beggining that is not letting me continue.
This is the site's url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
import login
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('login.urls', namespace='login')),
url(r'^admin/', include(admin.site.urls)),
)
And this is login/urls.py:
from django.conf.urls import patterns, url
from login import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^auth/', views.auth, name='auth'),
)
This is login/views,py
from django.shortcuts import render
from django.contrib.auth import authenticate
def auth(request):
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
# the password verified for the user
if user.is_active:
msg = "User is valid, active and authenticated"
else:
msg = "The password is valid, but the account has been disabled!"
else:
# the authentication system was unable to verify the username and password
msg = "The username and password were incorrect."
return render(request, 'login/authenticate.html', {'MESSAGE': msg})
def index(request):
return render(request, 'login/login_form.html')
I have a form that has this as action:
{% url 'login:auth' %}
And that's where the problem is, when I try to load the page, I get:
Reverse for 'auth' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$auth/']
But if I set the url pattern to
url(r'', views.auth, name='auth')
it works fine, only it sets the action as '/'.
I've been looking all around for an answer and I don't understand why it doesn't work.
I tried changing the login url pattern to url(r'^login/$', include('login.urls', namespace='login')), and it didn't change anything.
The problem is in the way you include the auth URLs in the main one.
Because you use both ^ and $, only the empty string matches. Drop the $.
I had html anchor tag: <a href="{% url 'url-name' url=someUUID %}">
in my urls.py
...
path('some_name/<slug:slug>/', views.someDetailView.as_view(), name='url-name'),
...
Solution for this:
This: <a href="{% url 'url-name' someUUID %}">
Or this: <a href="{% url 'url-name' slug=someUUID %}">
not: <a href="{% url 'url-name' url=someUUID %}">
DetailView uses slug_field = 'someUUIDField'
For some reason I wrote url instead of slug. Now it works.
Maybe you wrote wrong also, make sure you use slug=, if you use <slug:slug>.

Categories