multiple django urls arguments not working - python

urls.py
urlpatterns = [
url(r'^$', views.gallery, name='home'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
url(r'^(?P<album_id>\d+)/', views.album_details, name='album_details'),
]
views.py
def details(request, pic_id):
picture = get_object_or_404(Picture, pk=pic_id)
print("accessed details %s" %picture)
context = {
"picture": picture
}
return render(request, "picture_details.html", context)
gallery_details.html
{% for picture in pictures %}
<div class="img">
<a href="{% url 'gallery:details' picture.id %}">
<img src="{{ picture.picture_thumbnail.url }}" />
</a>
<div class="desc">{{ picture.description|truncatewords:5 }}</div>
</div>
{% endfor %}
When i try to run this i get an exception value:
Reverse for 'details' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['gallery/(?P<album_id>\\d+)/(?P<pic_id>\\d+)/$']
It should load page with single image, but it doesn't. Don't know why.

You may want to rearrange your urls so that the less specific one is first
url(r'^(?P<album_id>\d+)$', views.album_details, name='album_details'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
You will also note that I've modified the first regex to include the $ character to indicate thats the end of the match

Related

Django Error: NoReverseMatch at / ; looking at wrong urlpattern?

I am simply trying to open the web app to the home page, but at startup I am given the NoReverseMatch error message. The full message is the following: Reverse for 'wiki' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<title>[^/]+)/$']. In case it's relevant, I am using Django version 3.2.4.
In urls.py for my project I have:
urlpatterns = [
path('', include("encyclopedia.urls", namespace="encyclopedia")),
path('admin/', admin.site.urls),
]
In urls.py for my app I have:
app_name = "encyclopedia"
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<title>/", views.entry, name="wiki"),
.... (rest omitted for brevity)
]
In views.py I have:
def index(request):
context = {
"entries": ["item1", "item2", "item3"],
"form": SearchForm(),
}
return render(request, "encyclopedia/index.html", context)
In index.html, I have:
{% block body %}
<h1>All Pages</h1>
<ul>
{% for entry in entries %}
<li>
<form action="{% url 'encyclopedia:wiki' %}", method="POST">
{% csrf_token %}
<input type="text" value="{{ entry }}" name="link" hidden>
<input type="submit" class="link-btn" value="{{ entry }}" style="border: 0; background-color: white">
</form>
</li>
{% endfor %}
</ul>
{% endblock %}
As the error message indicates, it appears to be looking at the url pattern named wiki, and then subsequently failing to find the arguments passed to it. However, I am not trying to check that pattern; it should be going to the default pattern of path("", views.index, name="index"), not the wiki one. The wiki one works fine, by the way, as when I go the url wiki/<title>/, it loads fine. The rest of the app works fine as well. The other odd thing is that the index path worked fine before I added the arguments to the wiki url (path("wiki/<title>/", views.entry, name="wiki"),).
I have been looking at other SO posts that deal with this issue, and have followed the debugging advice given in this post, as well as this article, and none of the solutions apply/seem to work; this happens on app startup so there is no link being clicked to begin with, there are no comments it could be reading instead, it's not supposed to be calling wiki in the first place so the key word arguments are irrelevant, and the namespace is set in the project's urls.py. In summary, the issue appears to be that, upon start up of the app, it should be looking at the name "index" in the first path(), and then calling the appropriate function, but it is instead looking at the name "wiki" and expecting arguments.
If anyone can figure out how I can fix this problem, I would be very grateful!

NoReverseMatch during render

I'm receiving this error:
NoReverseMatch at /comments_page/1/post_comment/
Reverse for 'post_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comments_page/(?P[0-9]+)/post_comment/$']
My views.py
def post_comment(request, product_id):
host_product = Product.objects.get(pk=product_id)
comment = Comment()
comment.product = host_product
comment.author = request.POST["author"]
comment.comment_text = request.POST["comment"]
comment.save()
return render(request, 'comments_page/detail.html', {"host_product": host_product})
My comments_page\urls.py
from django.conf.urls import url
from . import views
app_name = "comments_page"
urlpatterns = [
# /comments_page/
url(r'^$', views.index, name="index"),
# /comments_page/1/
url(r'^(?P<product_id>[0-9]+)/$', views.detail, name="detail"),
# /comments_page/1/post_comment/
url(r'^(?P<product_id>[0-9]+)/post_comment/$', views.post_comment, name='post_comment'),]
My detail.html
<form action="{% url 'comments_page:post_comment' product.id %}" method="post">
{% csrf_token %}
Name: <input type="text" id="author" name="author">
Comment:
<textarea id="comment" name="comment"></textarea><br>
<input type="submit" value="Post Comment">
I think I've identified the problem as being in the product.id here
{% url 'comments_page:post_comment' product.id %}
in the html page. I've tried formatting this a couple of different ways, but I haven't had any luck. Do note, the comment is going through and the form and it works as far as updating the database and loading the entry on the page goes, but the page is not being redirected. I have to reload it manually. Any help would be appreciated.
The error message shows that the argument you pass to the {% url %} tag does not exist and resolves to an empty string. Your view indeed does not pass in a product variable, only a host_product variable. You need to change the tag accordingly:
{% url 'comments_page:post_comment' host_product.id %}
For those who may wonder, the fix is to change the return function in views.py to this
return render(request, 'comments_page/detail.html', {"product": host_product})
I do not understand why this works, but it does. Any suggestions as to how to clean up my post_comment function would be appreciated. I feel it's overly convoluted by using host_product

NoReverseMatch error Django 1.10

I am new to Django and cant seem to find a solution to my problem
I get the following error
Reverse for 'todo_list' with arguments '()' and keyword arguments
'{'cid': 1}' not found. 1 pattern(s) tried: ['todo/(?P<cid>)/']
1 {% extends "base.html" %}
2 {% block nav_title %} Company Batches {% endblock nav_title %}
3 {% block content %}
4 <div class="jumbotron">
5
6 {% for obj in object_list %}
7 <a href={% url 'todo_list' cid=obj.company.id%} class="href-nostyle">
8 <div class="container">
9 <div class="jumbotron" style="background:white">
10 <div class="text-center">
11 <h1>{{ obj.company }}<br>
12 <small>{{ obj.job }}</small>
13 </h1>
14 </div>
15 </div>
16 </div>
17 </a>
This template is located in an app named company_batches and I am attempting to navigate a user to the todo app using an href
my url tag is
{% url 'todo_list' cid=obj.company.id%}
my main urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', home, name='home'),
url(r'^batches/', include('company_batches.urls')),
url(r'^todo/', include('todo.urls'), name='todo')
]
todo/urls.py
urlpatterns = [
url(r'^$', ToDoCreateView.as_view(), name='todo_create'),
url(r'^(?P<cid>)/', ToDoListView.as_view(), name='todo_list'),
]
the relevant views.py
class ToDoListView(ListView,):
template_name = 'todo/todo_list.html'
def get_context_data(self, *args, **kwargs):
context = super(ToDoListView, self).get_context_data(*args, **kwargs)
return context
def get_queryset(self, cid):
return ToDoList.objects.filter(company=self.cid)
I cant figure out what I'm doing wrong, some guidance would be much appreciated
There are a few things to notice here.
Regular expression, probably the actual issue here
Capturing the cid in your url regex does not contain a proper capturing group. Since it's an ID, you should only capture digits with \d+
url(r'^(?P<cid>\d+)/', ToDoListView.as_view(), name='todo_list'),
Closing url regex
The current url does not contain a closing sign. If the url actually ends after the /app/<id>/, you should most likely close the regular expression with the dollar sign $.
url(r'^(?P<cid>\d+)/$', ToDoListView.as_view(), name='todo_list'),
Namespace usage
You are using a name while including the todo app urls. To use the namespace properly, you should drop the name in the todo/ url and add
the namespace to the include.
url(r'^todo/', include('todo.urls', namespace='todo'))
Now in your template you can use the namespace
{% url 'todo:todo_list' cid=obj.company.id %}
Your regex is broken; it doesn't have any characters to match on. It looks like you want to capture a numeric PK, do it should be:
r'^(?P<cid>\d+)/

"NoReverseMatch at /" when trying to create a link in template

I'm following the Django tutorial, and in the third part, they create a link in the template. Now, I followed and wrote a template like this:
<ul>
{% for blog in blogs %}
<li>
<a href="{% url 'detail' blog.id %}">
<h1>{{ blog.name }}</h1>
</a>
<p>{{ blog.description }}</p>
</li>
{% endfor %}
</ul>
And I have set the urls.py like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<blog_id>[0-9]+)/$', views.detail, name='detail'),
]
However, when I point my browser to the index, it gives me the following error:
NoReverseMatch at /
Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found.
1 pattern(s) tried: ['$(?P<blog_id>[0-9]+)/$']
The error details highlight this part of the template:
<a href="{% url "detail" blog.id %}">
What's going on in here? How do I fix this? I'm using Django 1.8.4.
You are mispelled in your main urls.py, something like.
url(r'^polls/$', include('polls.urls')),
You have to write like this
r'^polls/'. # Note please remove `$` in end

Django reverse error: NoReverseMatch

I've looked at a lot of different posts, but they're all either working with a different version of django or don't seem to work. Here is what I'm trying to do:
urls.py (for the entire project):
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
)
urls.py (specific to the app):
urlpatterns = patterns ('' ,
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>[\w\-]+)/$', views.posts, name="postdetail"),
)
views.py:
def index(request):
posts = Post.objects.filter(published=True)
return render(request,'blog/index.html',{'posts':posts})
def posts(request, slug):
post = get_object_or_404(Post,slug=slug)
return render(request, 'blog/post.html',{'post':post})
And finally the template:
{% block title %} Blog Archive {% endblock %}
{% block content %}
<h1> My Blog Archive </h1>
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url "postdetail" slug=post.slug %}">
{{post.title}}
</a>
</h2>
<p>{{post.description}}</p>
<p>
Posted on
<time datetime="{{post.created|date:"c"}}">
{{post.created|date}}
</time>
</p>
</div>
{% endfor %}
{% endblock %}
For some reason this gives me a "No reverse Match": Reverse for 'postdetail' with arguments '()' and keyword arguments '{u'slug': u'third'}' not found. 0 pattern(s) tried: []
I've already tried getting rid of the double quotes around postdetail in the template, and I've also tried referring to it by the view name instead of the pattern name. Still no luck. The documentation isn't too helpful either.
Help is really appreciated! Thanks
You've used a namespace when including the URLs, so you probably need to use "blog:postdetail" to reverse it.

Categories

Resources