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.
Related
I am using django-allauth for my authentication. I have a dashboard which has many links.
user.html
`{% include 'sidebar.html' %}
<h1>Profile View</h1>
<p>{{ profile.username }}</p>`
change_password.html
`{% include 'sidebar.html' %}
<h2>Change Password</h2>
<p>{{ profile.username }}</p>`
sidebar.html
`Profile View
Change Password`
views.py
class ProfileView(DetailView):
template_name = "user.html"
queryset = User.objects.all()
context_object_name = 'profile'
slug_field = "username"
change password view is from django-allauth. How can i pass the the username from the ProfileView to the change password view so that i can display in the change_password.html page.
Main Problem
Since i have been including sidebar.html to both the views, it works good in ProfileView but when i navigate to change_password view i get the following error
Reverse for 'profile_view' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['(?P[-\w.#+-]+)/$']
the error does not show in ProfileView because the profile.username returns a value, but when i navigate to change_password view i get the above error because the profile context object only passes in that particular view. i want to use that context_object through out the project.
Is there a simple way to do it? other than build a JSON API?
I just had a quick look at the template tags provided by the allauth app. I haven't tested this (nor do I use the project) but this looks like what you are looking for.
In your template, any template...
{% load account %} {# this should be near the top with any other load tags #}
{# this can be anywhere in the template. I'm not positive what it returns but it looks like it is the username. #}
<p>
{% user_display user %}
</p>
I found the answer here.
I got it working, Thank you #teewuane
sidebar.html
Profile View
replaced as
Profile View
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 %}
in views.py
class LaViewSet(viewsets.ModelViewSet):
serializer_class = IlSerializer
def get_queryset(self):
ilfiltro = self.kwargs['miopar']
return models.Pippo.objects.filter(pippo=ilfiltro)
in url.py
url(r'^pippo/(?P<miopar>.+)', views.LaViewSet.as_view({'get': 'list'}), name="Serializzata"),
this is a working url:
http://127.0.0.1:8000/pippo/1
but if I put in a template:
{% url '1' 'Serializzata' %};
or
{% url 'Serializzata'?1 %};
keep getting this error:
TemplateSyntaxError: Could not parse the remainder: '?1' from
''Serializzata'?1'
From the docs:
url
Returns an absolute path reference (a URL without the domain name)
matching a given view and optional parameters. Any special characters
in the resulting path will be encoded using iri_to_uri().
This is a way to output links without violating the DRY principle by
having to hard-code URLs in your templates:
{% url 'some-url-name' v1 v2 %}
So in your case:
{% url 'Serializzata' 1 %}
Try this:
<a href="{% url 'Serializzata' 1 %}">
I am attempting to create a simple forum application and am getting this error. I am inclined to think this may have something to do with my URLConf or possibly the way I am using the URL template tag to generate the URL for each thread.
/forums/urls.py
# ex: /forums/general_forum
url(r'^(?P<forum_slug>[-\w\d]+)/$', views.forum, name='forum'),
# ex: /forums/general_forum-1/my_first_thread
url(r'^(?P<forum_slug>[-\w\d]+)/(?P<thread_slug>[-\w\d]+)/$', views.thread, name='thread'),
/forums/views.py
The index view works fine, the forum view does not.
def index(request):
context = RequestContext(request)
forum_list = Forum.objects.order_by("sequence")
for forum in forum_list:
forum.url = slugify(forum.title) + "-" + str(forum.id)
context_dict = {'forum_list': forum_list}
return render_to_response('forums/index.html', context_dict, context)
#login_required
def forum(request, forum_slug):
context = RequestContext(request)
try:
forum = Forum.objects.get(slug=forum_slug)
threads = Thread.objects.filter(forum=forum)
context_dict = {'forum': forum,
'threads': threads}
except Forum.DoesNotExist:
pass
return render_to_response('forums/forum.html', context_dict, context)
This is how I am linking to the forum view inside index.html.
<a href={% url 'forum' forum.slug %}>{{ forum.title }}</a>
And in forum.html, this is how the links are formulated to view the posts inside the thread. This :
<a href={% url 'forum' forum.slug %}/{% url 'thread' thread.slug %}>{{ thread.title }}</a>
The error. One of the threads is titled 'django'
NoReverseMatch at /forums/web-development/
Reverse for 'thread' with arguments '(u'django',)' and keyword arguments '{}' not found. 2 pattern(s) tried: ['forums/(?P<forum_slug>[-\\w\\d]+)/(?P<thread_slug>[-\\w\\d]+)/$', '$(?P<forum_slug>[-\\w\\d]+)/(?P<thread_slug>[-\\w\\d]+)/$']
In the error, the url template tag for 'thread' is highlighted in red and states there was an error during template rendering. This error seems unclear to me and I am not sure if this is an issue with the way I am using the template tags or something else.
You're not passing the Forum slug in your thread URL, which your URL configuration requires:
<a href={% url 'forum' forum.slug %}/{% url 'thread' thread.slug %}>{{ thread.title }}</a>
You should not be using both urls, either. Instead, what you want is:
{{ thread.title }}
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 %}