Template for reusable Django app is not rendering - python

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.

Related

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

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

Django Error passing URL argument from Template to View: NoReverseMatch Reverse not found. 1 pattern(s) tried

I am trying to pass configure a URL like so:
/details/12345
Template HTML:
<div class="row">
{% if article_list %}
{% for article in article_list %}
<div>
<h2>{{ article.title }}</h2>
<p>{{ article.body }}</p>
<p><a class="btn btn-default" href="{% url 'details' article.id %}" role="button">View details »</a></p>
</div><!--/.col-xs-6.col-lg-4-->
{% endfor %}
{% endif %}
</div><!--/row-->
urls.py (full):
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'news_readr.views.home', name='home'),
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py:
from django.shortcuts import render
from .models import Article
# Create your views here.
def home(request):
title = "Home"
article_list = Article.objects.all()
for article in article_list:
print(article.id)
context = {
"title": title,
"article_list": article_list,
}
return render(request, "home.html", context)
def details(request, article_id = "1"):
article = Article.objects.get(id=article_id)
return render(request, "details.html", {'article': article})
I am getting an error that says:
NoReverseMatch at /
Reverse for 'details' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/(?P<article_id>\\d+)/$']
I'm one week old at Django, and I think there's something wrong with my URL Named Group config. Please help! TIA!
Update: If I remove the URL config and change it back to:
url(r'^details/$', 'news_readr.views.details', name='details'),
The error changes to:
Reverse for 'details' with arguments '(1,)' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/$']
So it seems to be picking up the argument that is passed 1 in this case. So this seems to be an issue with the regular expression. I tried the expression out at Pythex, but even there, the expression doesn't seem to be matching anything.
For the url pattern
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
The correct way to use the tag is
{% url 'details' article.id %}
This because the details url pattern has a group article_id, so you have to pass this to the tag.
If you have the above url pattern, and {{ article.id}} displays correctly in the template, then the above template tag should not give the error Reverse for 'details' with arguments '()'. That suggests you have not updated the code, or you have not restarted the server after changing code.
If you change the url pattern to
url(r'^details/$', 'news_readr.views.details', name='details')
then you need to remove the article.id from the url tag.
{% url 'details' %}
I guess your pattern is wrong.( not an expert of regex ).
Try this
url(r'^details/((?P<article_id>[0-9]+)/$', 'news_readr.views.details', name='details'),

NoReverseMatch pattern not found

Having trouble figuring out why I keep getting the NoReverseMatch.
app/url.py
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<contact_id>\d+)/detail/$', views.details, name='details'),
)
views.py
from django.core.urlresolvers import reverse
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.http import Http404
from django.views import generic
from django.template import RequestContext, loader
from .models import Person
# Create your views here.
class IndexView(generic.ListView):
template_name = 'ContactManager/index.html'
context_object_name = 'contact_list'
def get_queryset(self):
return Person.objects.order_by('lname')
def details(request, contact_id):
contact = get_object_or_404(Person, id=contact_id)
return render(request, 'ContactManager/details.html', {'contact': contact})
# class DetailView(generic.ListView):
# model = Person
# context_object_name = 'contact'
# template_name = 'ContactManager/details.html'
#
# def get_queryset(self):
#
template index.html
{% if contact_list %}
<ul>
{% for contact in contact_list %}
<li>
{{ contact.fname }} {{ contact.lname }}
</li>
{% endfor %}
</ul>
{% else %}
<p>You don't have any contacts currently.</p>
{% endif %}
The error I am getting:
Reverse for 'details' with arguments '()' and keyword arguments '{'contact_id': 1}' not found. 1 pattern(s) tried: ['$(?P<contact_id>\\d+)/detail/$']
I have tried using generic views and a host of arguments in the {% url ... %}
Any help would be much appreciated.
I think that details url pattern has a mistake, in error message appears one tried pattern, started and ended by $ sign:
1 pattern(s) tried: ['$(?P\d+)/detail/$']
check your pattern that is equal to or no:
^(?P<contact_id>\\d+)/detail/$
if this is correct check your urls file that included contact urls and if is similar below:
url('^$', include(ContactManager.urls, namespace='contact'))
remove $ sign at end of prefix-pattern:
url('^', include(ContactManager.urls, namespace='contact'))
Note that this error can also arise if you do not define name in your urlpatterns.
Doing something like
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^error_containing_view/$', views.error_containing_view, name='error_containing_view'),
]
in your app's urls.py would fix the error.

Reverse for '' with arguments '()' and keyword arguments '{}' not found

The error points to the {% url product %}
NoReverseMatch at /category/
Reverse for '' with arguments '()' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://127.0.0.1:8000/category/
Django Version: 1.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for '' with arguments '()' and keyword arguments '{}' not found.
Exception Location: C:\Python27\lib\site-packages\django\template\defaulttags.py in render, line 424
Python Executable: C:\Python27\python.exe
Python Version: 2.7.2
My HTML
<html>
<body>
<p> The list of items are </p>
{% for items in allobs %}
<li>{{items}}</li>
{% endfor %}
<p>Product list</p>
</body>
</html>
My VIEWS
from django.http import HttpResponse, Http404
from models import Category, Product
from datetime import datetime, timedelta, date, time
from django.shortcuts import render_to_response
def hello(request):
return HttpResponse("Hello World")
def category(request):
cat = Category.objects.all()
return render_to_response('category.html',{'allobs': cat})
def product(request):
pro = Product.objects.all()
return render_to_response('product.html',{'allobs':pro})
MY URLS
from django.conf.urls.defaults import *
from django.contrib import admin
from website.views import hello, category, product
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', hello),
url(r'^category/$', category),
(r'^tinymce/', include('tinymce.urls')),
url(r'^product/$', product),
)
if settings.DEBUG:
urlpatterns += patterns('django.views.static',
(r'images/(?P<path>.*)', 'serve', {'document_root': settings.MEDIA_ROOT}),
)
Guess {% url product %} should be {% url 'product' %} if product is the name of your view.
Edit: Also use the full name of the view 'product.views.product'.

Categories