ok I was getting help from a kind person on this problem and would like to see if anyone can help here is my code.
urls.py
urlpatterns = patterns('',
url(r'^venues/(?P<venue_id>\d+)/$','venues.views.venue', name='venue'),
views.py
def venue(request,venue_id):
venue= get_object_or_404(VenueProfile,
venue__pk=venue_id).select_related('venue')
return render_to_response('venues/venueprofile',{'venue',venue},
context_instance=RequestContext(request))
template
{% load url from future %}
{% for v in venues %}
{% with ven=v.venue profile=v %}
{{ven.name}}
now when I try to use this i get Reverse for 'venue' with arguments '()' and keyword arguments '{'venue_id': ''}' not found.Can anybody please help me here, and please keep in mind I left out some code like the {{venues}} object defined in a view.
Your venue object in your template is none, so it doesn't have a pk. In your template you aren't passing any venues object either. Perhaps simplifying your code a bit will help:
Adjusting your view method a bit:
from django.shortcuts import render
def venue(request,venue_id):
the_venue = get_object_or_404(Venue,pk=venue_id)
return render(request,'venues/venueprofile',{'venue',the_venue})
Your template:
{% load url from future %}
{{venue.name}}
The value of venue.pk is empty string, since it does not match your regular expression, it is showing no reverse match problem.
{% load url from future %}
{% for v in venues %}
{% with ven=v.venue profile=v %}
{{ven.name}} # Problem is that venue.pk is empty string
Reverse for 'venue' with arguments '()' and keyword arguments
'{'venue_id': ''}'
check here value of venue_id is ''.
Replace
{% url 'venue' venue_id=venue.pk %}
with
{% url 'venue' venue_id=ven.pk %}
Related
I am able to print the correct object pk in the template using a template tag, but when I use the same code in a url parameter it does not show up.
I am trying to using the first result pk from a many to many relationship to create a url parameter to link to that page. It works when I manually input the pk, but when I use category.quote_set.first.pk it does not work.
"category" is is queryset of all categories, which have a many to many relationship to quotes.
<p>{{ category.quote_set.first.pk }}</p>
<p></p>
The url file has path('motto/<int:pk>/', views.QuoteView.as_view(), name='quote'),
Going to the page shows an error Reverse for 'quote' with arguments '('',)' not found. 1 pattern(s) tried: ['motto\\/(?P<pk>[0-9]+)\\/$']
I believe the reason for this is that the url is created first, and the category.quote_set.first.pk is created after the page, but that is just my theory.
View for the page:
class CategoryView(generic.ListView,ContextMixin):
template_name = 'mottos/category.html'
context_object_name = 'motto_list'
def get_queryset(self):
return Quote.objects.all().annotate(score=Avg('vote__score')).filter(categories__slug=self.kwargs['cat
egory']).order_by('-score')
Try something like this
{% for quote in quote_list %}
<p>
</p>
{% endfor %}
Another solution:
in you views fie add this:
def get_context_data(self, **kwargs):
context['quote_list'] = Quote.objects.all().annotate(score=Avg('vote__score')).filter(categories__slug=self.kwargs['category']).order_by('-score')
return context
Then in your template add this
{% for quote in quote_list %}
<p>
</p>
{% endfor %}
I was able to get the quote pk by using
{% for quote in category.quote_set.all|slice:"0:1" %}
<p></p>
% endfor %}
Since I only wanted the first quote, I used slice:"0:1" to only get the first quote, and then used the pk from that result.
I got an error,
TypeError at /app/^detail/(?P1[0-9]+)/$
detail() got an unexpected keyword argument 'pk'
.
I wrote urls.py
urlpatterns = [
path('top/', views.top, name='top'),
path(r'^detail/(?P<pk>[0-9]+)/$',views.detail , name='detail'),
]
in views.py
def top(request):
content = POST.objects.order_by('-created_at')
page = _get_page(blog_content, request.GET.get('page'))
return render(request, 'top.html',{'content':content,"page":page})
def detail(request):
content = POST.objects.order_by('-created_at')
return render(request, 'detail.html',{'content':content})
in top.html
<div>
{% for content in page %}
<div>
<h2>{{ content.title }}</h2>
</div>
{% endfor %}
</div>
<div>
{% for content in page %}
<h2>{{ content.title }}</h2>
<p>SHOW DETAIL</p>
{% endfor %}
</div>
When I put "SHOW DETAIL" button, this error happens.I really cannot understand why I can't access pk. pk is default value, so I think I can access it from everywhere.I wanna make a system when I put "SHOW DETAIL button",content's detail is shown.What is wrong in my code?How should I fix this?Am I wrong to write url?Or is this error's meaning I should write pk in detail method?
You're using the new path() function, which does not take a regex. Either go back to the old url() function, or use <type:name> syntax:
path('detail/<int:pk>/', ...)
In the view function detail(), there's no argument named pk. Add it as the second argument should solve your problem:
def detail(request, pk):
I'm trying to make my pagination url's from django a little bit SEO friendly. Instead of ?page=current_page something of the form /page/current_page.
So in my app/urls.py I did the following:
url(r'^(?P<slug>[a-zA-Z0-9-_]+)/page/(?P<page>[0-9])+$', GalleryDetail.as_view(), name='galleries-view-gallery-paginator')
and on my app/templates/app/my_view.html:
{% if page_obj.has_next %}
next
{% endif %}
But I get a NoReverseMatch error.
Other error info: Reverse for 'galleries-view-gallery-paginator' with arguments '(2,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['gallery/(?P<slug>[a-zA-Z0-9-_]+)/page/(?P<page>[0-9])+$']
And well. How can I achieve urls like /page/current_page using Django?
URL galleries-view-gallery-paginator requires you to pass 2 parameters: slug and page. Since you are passing only the page number, slug is also needed:
{% url 'galleries-view-gallery-paginator' gallery.slug page_obj.next_page_number %}
quite new to Django and Python. Hoping to get some help on what went wrong with the below code. Basically, I am trying to create a url with and id and a slugfield e.g. something.com/1/arts-and-crafts.
Stucked and getting a NoReverseMatch error below:
Reverse for 'category' with arguments '(2, u'arts-and-crafts')' and keyword arguments '{}' not found. 2 pattern(s) tried: ['(?P\d+)/[-\w]+/$', '(?P\d+)/$']
Below is the code that I am using, would greatly appreciate any help in understanding what went wrong:
template html
<ul class="list-unstyled">
{% for category in categories %}
{% if forloop.counter < 10 %}
<li><a href="{% url 'category' category.id category.slug %}">
{{ category.cat_name}}</a></li>
{% endif %}
{% endfor %}
</ul>
url.py
url(r'^(?P<cat_id>\d+)/[-\w]+/$', 'guides.views.category', name='category'),
views.py
def category(request, cat_id):
categories = Category.objects.all()
guides = Guide.objects.filter(category__id=cat_id).order_by("created_date").reverse()
is_featured = Guide.objects.filter(category__id=cat_id).filter(featured=True)
selected_category = get_object_or_404(Category, pk=cat_id)
return render(request, 'guides/category.html', {'categories': categories, 'guides': guides, 'is_featured': is_featured, 'selected_category': selected_category})
Your URL doesn't capture the slug as a parameter, so it can't be used in the reverse call. Either change the second pattern to (?P<slug>[-\w]+) or don't use category.id in the {% url %} tag.
I am getting an error creating a link in my Django template.
My template looks like this:
{{ location.name }}
My urls.py looks like:
url(r'^location(?P<pk>\d+)/$', views.location_detail, name="location_detail"),
My view looks like:
def location_detail(request, pk=None):
I get the error:
Reverse for views.location_detail with arguments '()' and keyword arguments '{u'pk': 1L}' not found.
I'm using Django 1.5 and python 2.7.2
Thanks!
The problem was that I had a name space on the primary project urls.py:
url(r'^com/', include('com.urls', namespace="com")),
Changing the url to:
{% url 'com:location_detail' pk=location.id %}
That did the trick
You have given your url pattern a name, so you should use that name in the {% url %} call:
{% url 'location_detail' pk=location.id %}