Getting NoReverseMatch Error - python

I have an app that is going to display some information about people in my group. I'm getting a NoReverseMatch error when trying to use the url tag in my index.html. If I do not use the url tag, but specify the root, I do not receive the error.
The error says:
NoReverseMatch at /
Reverse for 'specialist' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$(?P[0-9]+)/']
Here is are the urls.py files.
From the main wi_tech urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('person.urls')),
url(r'^tech/', include('person.urls')),
url(r'^admin/', admin.site.urls),
]
From the 'person' app urls.py:
from django.conf.urls import url
from . import views
app_name = 'person'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='specialist'),
]
My views.py file looks like this:
from django.views import generic
from .models import Specialist
class IndexView(generic.ListView):
template_name = 'person/index.html'
context_object_name = 'person_list'
def get_queryset(self):
"""Return all specialists"""
return Specialist.objects.order_by('id')
class DetailView(generic.DetailView):
model = Specialist
template_name = 'person/detail.html'
And my index.html page looks like this:
{% if person_list %}
<ul>
{% for specialist in person_list %}
<li> {{ specialist }}</li>
{% endfor %}
</ul>
{% else %}
<p>No specialists are available.</p>
{% endif %}
If I change my tag in index.html to this, it works:
<li> {{ specialist }}</li>
Obviously this isn't an ideal situation in case the web root ever changes. I've reviewed a lot of SO questions on this, and nothing seems to match. I think the issue is the "$" in the beginning of the regex, but I don't see where that's coming from.
Specifically, I used this link as a really good reference point, but came up empty looking through my code.
what is NoReverseMatch and how do i fix it
Clearly there's something I'm missing.

Could it be the additional / at the end of the URL?
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='specialist')
The one after your primary key modifier. When you click the URL (with the (% url ':' model.name %)) what URL comes up in your browser?

I ended up scrapping this implementation, and going with a flatter structure whereby all models, views and templates are in the same application. I have not had this problem in the new design.

Related

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 in Django (url ploblem)

I'm a beginner of Django
I want to set my url with database field_name instead of use primary key from Django tutorial. This is my code.
*mysite*
**dwru/urls.py**
urlpatterns = [
url(r'^$', include('product.urls', namespace="product")),
]
*myapp*
**product/urls.py**
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<name_catalog>[-_\w]+)/$', views.product_list_from_index, name='catalog'),
]
**product/views.py**
def product_list_from_index(request, name_catalog):
catalog = get_object_or_404(Catalog, name_catalog=name_catalog)
context = {
'catalog': catalog
}
return render(request,'product/product_list.html', context)
**product/models.py**
class Catalog(models.Model):
name_catalog = models.CharField(max_length=45)
product = models.ManyToManyField(Product)
**template/product/index.html**
{% for catalog in catalog_list %}
<h1><a href="{% url 'product:catalog' catalog.name_catalog %}">{{ catalog.name_catalog }}</h1>
{% endfor %}
Then I add Catalog field with "TestCa01" then it shown an error
NoReverseMatch at /
Reverse for 'catalog' with arguments '(u'TestCa01',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$(?P<name_catalog>[-_\\w]+)/$']
it kind of some problem with this line in template
{% url 'product:catalog' catalog.name_catalog %}
any Idea?
The $ in your first url pattern might be the issue. The dollar sign indicates that it's the the end of the pattern, so it would never bother to include/check the product urls. Is the index url working?

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.

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

Django template not displaying

I have a template for an about page which refuses to show. I'm probably doing something silly but I can't work out why and it's driving me insane!
part of views.py:
# About view
def about(request):
return render(request, 'blog/about.html')
urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog.views import post as blog_post
from blog.views import profile as blog_profile
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.index'),
url(r'^about/$', 'blog.views.about'),
url(r'^profiles/$', 'blog.views.profile_index'),
url(r'^profiles/(?P<profile_url>[\w\-]+)/$', blog_profile, name = 'blog_profile'),
url(r'^(?P<category>[\w\-]+)/$', 'blog.views.categoryIndex'),
url(r'^(?P<category>[\w\-]+)/(?P<slug>[\w\-]+)/$', blog_post, name = 'blog_post')
)
the about template (not including the base.html):
{% extends 'base.html' %}
{% block title %} About {% endblock %}
{% block content %}
<h1>About</h1>
{% endblock %}
Using Django 1.6.5
Tried to navigate to mysite.com/about/
Template hierachy:
templates
base.html
blog
about.html
....
If your templates dir is at the root level of the project, you might want to add the following to the settings.py
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
I've figured out the problem, the following url was being matched with the about page instead:
url(r'^(?P<category>[\w\-]+)/$', 'blog.views.categoryIndex'),
Removing this solves the issue and displays the about page. This page explains about url ordering http://www.webforefront.com/django/regexpdjangourls.html
Because my about url was listed before the conflicting one, it was skipped.

Categories