Django: Make pagination's urls SEO friendly - python

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

Related

Django can't figurout how to pass paramater from url

I am trying to pass parameter from url. I have tried many tutorials and can't figure out what am i doing wrong.
my url from url.py:
url(r'^reports/(?P<test>\d+)/$', views.reports,name='reports'), # report view
my view from view.py:
def reports(request, test=0 ):
title = "Reports" # title shown in browser window
view ="admin/pc/reports.html"# where is the page view
user_name = request.user.get_username() #User name
return render(request, 'admin/home.html', {"title":title,"USER_NAME" : user_name,"page" : view, 'pid':test})
and my template:
{% block content %}
REPORTSz id = {{pid }}
{% endblock %}
but no matter what I do I always get:
Reverse for 'reports' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['admin/reports/(?P\d+)/$']
So my question is how to correctly pass parameter from url?
In url tag in Django templates, you need to pass your test parameter:
href="{% url "reports" test="some_value" %}"
because test parameter is required in your URL.

NoReverseMatch Error when adding slug to url

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.

named url not working in django 1.5 and python 2.7

I'm developing my own blog. Everything was good. I tried prepare it for deployment which was not successful. Now I undo all changes, but named url not working now (they were worked perfectly before):
error:
Reverse for ''main_page'' with arguments '()' and keyword arguments '{}' not found
url:
urlpatterns = patterns('',
url(r'^$', main_page, name='main_page'),
url(r'^blog/', include('blog.blogurls')),
url(r'^comments/', include('django.contrib.comments.urls')),
)
main page view:
def main_page(request):
object_list = Article.objects.all()
return render_to_response('blog/main_page.html', {'Latest': object_list}
named url used in:
<p>home</p>
Replace {% url 'main_page' %} with {% url main_page %}.
Quote from django 1.5 changelog:
One deprecated feature worth noting is the shift to “new-style” url
tag. Prior to Django 1.3, syntax like {% url myview %} was interpreted
incorrectly (Django considered "myview" to be a literal name of a
view, not a template variable named myview). Django 1.3 and above
introduced the {% load url from future %} syntax to bring in the
corrected behavior where myview was seen as a variable.
The upshot of this is that if you are not using {% load url from
future %} in your templates, you’ll need to change tags like {% url
myview %} to {% url "myview" %}. If you were using {% load url from
future %} you can simply remove that line under Django 1.5

Receiving error: Reverse for with arguments '()' and keyword arguments not found

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

django no reverse match

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

Categories

Resources