No Reverse Match with Django Auth Views - python

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' %}">

Related

How to switch HTML page in Python with Django

Template :
<a class="btn btn-primary" href="{% url 'edit' %}">Edit</a>ù
views.py:
def edit(request):
return render(request, "edit.html")
urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("wiki/create", views.create, name="create"),
path("wiki/edit", views.edit, name="edit"),
path("wiki/<str:name>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("save", views.save, name="save"),
path("random", views.random, name="random"),
]
I would like simply to switch from an HTML page on another, but the function gives me this error:
TemplateDoesNotExist at /wiki/edit
edit.html
But the template exist, I created it. I tryed a lot of changes but all of them gives me error. Thank you.
The Application name needs to be referenced in the template name.
Like so:
return render(request, "encyclopedia/edit.html")

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.

NoReverseMatch in Django 1.10

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' %}

Passing an id to a url link - Django

I need to pass a cluster_id in my show.html page, in case the user wishes to halt a cluster created. My urls.py looks like:
from django.conf.urls import url
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from clusters import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/', views.register, name='register'),
url(r'^show/', views.show, name='show'),
url(r'^login/', views.user_login, name='login'),
url(r'^logout/', views.user_logout, name='logout'),
url(r'^destroy_clusters/', views.destroy_clusters, name='destroy_clusters'),
url(r'^halt_clusters/(?P<cluster_id>\d+)/$', views.halt_clusters, name='halt_clusters'),
url(r'^check_log_status/', views.check_log_status, name='check_log_status'),
url(r'^read_cluster_log/', views.read_cluster_log, name='read_cluster_log'),
]
and in my show.html I have:
{% for cluster in clusters %}
<tr>
<td>{{ cluster.id }}</td>
<td>Halt</td>
<tr>
{% endfor %}
so when I execute the webpage, I have:
NoReverseMatch at /clusters/register/
Reverse for '' with arguments '()' and keyword arguments '{u'cluster_id': 19L}' not found. 0 pattern(s) tried: []
and this is driving me crazy!! Why can't I just pass the id there? I looked this example: Passing an id in Django url but it doesn't seem to work for me. Help?
My Django version is 1.8.dev
In recent Django's you should quote literal URL names in the tag:
{% url "halt_clusters" cluster_id=cluster.id %}
Otherwise Django will look for halt_clusters variable in your context and use its value as the URL name.
Oh, and while you're at it, you don't have to specify the keyword parameter name, so this should work as well:
{% url "halt_clusters" cluster.id %}

NoReverseMatch and urls.py

I am getting a TemplateSyntaxError whenever I try to run a search using gitpaste:
<a href='{% url owner_pastes owner=result.object.owner.pk %}'>
{{ result.object.owner }}
</a>
The urls.py is laid out as:
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
urlpatterns = patterns('saic.paste.views',
url(r'^live/$', 'live_paste', name='live_paste'),
url(r'^(?P<pk>\d+)/$', redirect_to, {'url': '/paste/%(pk)s/'}),
url(r'^(?P<pk>\d+)/(?P<private_key>[a-zA-Z0-9]+)?/?$', redirect_to, {'url': '/paste/%(pk)s/%(private_key)s/'}),
url(r'^owner/$', redirect_to, {'url': '/owner/all/'}),
url(r'^owner/anonymous/', 'user_pastes', name='anon_pastes'),
url(r'^owner/(?P<owner>.+)/', 'user_pastes', name='user_pastes'),
url(r'^paste/(?P<pk>\d+)/adopt/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_adopt', name='paste_adopt'),
url(r'^paste/(?P<pk>\d+)/embed/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_embed', name='paste_embed'),
url(r'^paste/(?P<pk>\d+)/edit/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_edit', name='paste_edit'),
url(r'^paste/(?P<pk>\d+)/fork/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_fork', name='paste_fork'),
url(r'^paste/(?P<pk>\d+)/favorite/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_favorite', name='paste_favorite'),
url(r'^paste/(?P<pk>\d+)/delete/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_delete', name='paste_delete'),
url(r'^paste/(?P<pk>\d+)/raw/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_raw', name='paste_raw'),
url(r'^commit/(?P<pk>.+)/adopt/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'commit_adopt', name='commit_adopt'),
url(r'^commit/(?P<pk>.+)/download/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'commit_download', name='commit_download'),
url(r'^paste/(?P<pk>\d+)/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_view', name='paste_view'),
url(r'^users/$', 'users', name='users'),
url(r'^favorites/$', 'favorites', name='favorites'),
url(r'^accounts/login/$', 'login', name='login'),
url(r'^accounts/logout/$', 'logout', name='logout'),
url(r'^accounts/register/$', 'register', name='register'),
url(r'^accounts/preference/$', 'preference', name='preference'),
url(r'^accounts/timezone/$', 'set_timezone', name='set_timezone'),
url(r'^$', 'paste', name='paste'),
)
From reading the Django documentation, it seems that I need a url pattern for owner. This is the rest of the error I am getting:
Caught NoReverseMatch while rendering: Reverse for 'owner_pastes' with
arguments '()' and keyword arguments '{'owner': 5}' not found.
It looks like the URL is there. What is wrong?
The name of the URL is not owner_pastes but user_pastes according to the following line:
url(r'^owner/(?P<owner>.+)/', 'user_pastes', name='user_pastes'),
Try this:
<a href='{% url user_pastes owner=result.object.owner.pk %}'>{{ result.object.owner }}</a>
Side-note: be careful, with Django >= 1.5, you need to surround the first parameter by quotes (otherwise it is treated as a variable):
<a href='{% url "user_pastes" owner=result.object.owner.pk %}'>{{ result.object.owner }}</a>

Categories