The purpose of this section of code is to show all of the requests to join a group in a template similar to that shown below:
Request 1 | Add | Delete
Request 2 | Add | Delete
Request 3 | Add | Delete
....
What I have thought to do is to to make the 'add' and 'delete' button href's to a function in the view. However I am wondering what the proper way to pass a **kwarg from a template to a view. Else if there is any better way to accomplish this?
template
{% for asking in requested %}
<a href={% url 'group_judge_request' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
url
url(r'^judge_request/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision':'decision'},
name='group_judge_request'),
view group_judge_restart
def group_judge_request(request, gslug, decision):
view group_requested_invites
def group_requested_invites(request, gslug):
....
requested = GroupRequestToJoin.objects.filter(group=group_profile.group, checked=False)
return render(request, "groups/group_requested_invites.html", {
'requested' : requested,
})
Error:
Don't mix *args and **kwargs in call to reverse()!
I don't think there is a way to pass *kwargs like this from the template using the built-in url template tag.
There are two ways you can do this, one is to create two separate urls and pass the decision as a kwarg to the view:
urls.py
url(r'^judge_request_cut/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision': 0},
name='group_judge_request_cut'),
url(r'^judge_request_keep/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision': 1},
name='group_judge_request_keep'),
template
{% for asking in requested %}
<a href={% url 'group_judge_request_cut' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request_keep' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
Or you could pass the integer as a parameter:
urls.py
url(r'^judge_request/(?P<gslug>[\w-]+)/(?P<decision>\d{1})$',
group_judge_request,
name='group_judge_request'),
template
{% for asking in requested %}
<a href={% url 'group_judge_request' group_profile.slug 0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request' group_profile.slug 1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
I think you want to use a url query. So your url will be as follows:
Cut {{asking.user.username }}
You can then go on to list the queries using:
request.META['QUERY_STRING']
Related
VIEWS.PY
def index(request):
posts = Post.objects.all()
posts_quantity = range(1)
return render(request, 'index.html', {'posts':posts, 'posts_quantity':posts_quantity})
HTML
{% for index in posts_quantity %}
<a href = "{% url 'architectural_post' posts.index.slug %}" class = "post">
{% endfor %}
it gives me an error: Reverse for 'architectural_post' with arguments '('',)' not found.
But everything works well if I put 0 instead of index, like this (just for debugging):
{% for index in posts_quantity %}
<a href = "{% url 'architectural_post' posts.0.slug %}" class = "post">
{% endfor %}
Why and how to fix it?
Thanks.
Django is not interpreting posts.index.slug as posts.0.slug. Instead, Django is trying to find an attribute called index on the posts object.
It's not very intuitive, but that's how it is.
You can fix it by iterating over the posts directly:
{% for post in posts %}
<a href="{% url 'architectural_post' post.slug %}" class="post">
{% endfor %}
When i clicked my menu url only gets category name and slug cant reach id how can i solve that ?
This is the url i get
def category_products (request,id,slug):
category=Category.objects.all()
products=Product.objects.filter(category_id=id)
context={'products':products,'category':category,'slug':slug, }
return render(request, 'kiliclar.html', context)
urlpatterns = [
path('category/<int:id>/<slug:slug>/', views.category_products,name='category_products'),
]
template
{% recursetree category %}
<li class="dropdown">
{{ node.title }}
{% if not node.is_leaf_node %}
<ul class="dropdown-menu">
<li>{{ children }}</li>
</ul>
{% endif %}
How does Django get id on url?
You need to supply it when you generate the "href" in the template.
The URL pattern says:
path('category/<int:id>/<slug:slug>/',
views.category_products,
name='category_products')
So the URL in the href in the template needs to look the same as the pattern:
href="/category/{{ node.id }}/{{ node.slug }}"
Or better still use the 'url' template function to expand the url from the pattern:
href="{% url category_products id=node.id slug=node.slug %}"
Here is a similar Q&A:
How to add url parameters to Django template url tag? to give you another example.
You need to add one more argument, it's id.
{{ node.title }}
It should be;
{{ node.title }}
I have been trying to solve this problem: i want to set url to html a tag using loop. I tried this way. But it gives me error which is "Reverse for 'i.menuResolve' not found. 'i.menuResolve' is not a valid view function or pattern name".
In case, "i.menuResolve" returns url which is '/sales/profile' etc.
{% for i in userMenus %}
<li>
<a href="{% url 'i.menuResolve' %}" ></a>
</li>
{% endfor %}
Please help if anybody knows this error?
If you have a method or property that returns a URL, you don't need to use Django's {% url %} template tag. That template tag passes the arguments to Django's reverse() function, but you don't need to do that if you've already got the URL.
Give this a try:
{% for i in userMenus %}
<li>
<a href="{{ i.menuResolve }}" ></a>
</li>
{% endfor %}
I have this requirement in django template:
{% if user.is_admin %}
<a href='{% url 'url_to_admin' %}'>admin url</a>
{% else %}
<a href='{% url 'url_to_other %}'>other url </a>
{% endif %}
and now, I want let it more simple, I want let url to a variable `xx', like this:
if user.is_admin:
myurl = {% url 'url_to_admin' %}
else:
myurl = {% url 'url_to_other' %}
<a href={{myurl}} > url </a>
But I don't know how to write in django's template?
The url tag can be assigned to a variable through using as.
{% url 'my_url' as foobar %}
{{ foobar }}
But that doesn't make sense in the current place where you're using it. Just assign the anchor tags inside of the if/else statements
{% if user.is_admin %}
admin
{% else %}
other
{% endif %}
Not the answer, but you may try this:
<a href='{% if user.is_admin %}{% url 'url_to_admin' %}{% else %}{% url 'url_to_other' %}'>other url </a>
why don't you handle it in the views before you render the template, try something like in your view.py
if User.is_admin:
url = 'url_to_admin'
else:
url = 'url_to_other'
render(request, "your_template.html", {'url' : url})
then you can go ahead and call your url in your template like
<a href= "{{ url }}>url</a>"
views.py
from django.core.urlresolvers import reverse
def my_view(request):
if request.user.is_admin:
my_url = reverse('my_admin_view')
else:
my_url = reverse('my_other_view')
return render(request, 'my_template.html', {'my_url': my_url })
Using django.core.urlresolvers.reverse
my_template.html
This is a link
I am suddenly getting a NoReverseMatch error in a production Django webapp. This code has not changed in months. What could be causing this?
URL
urlpatterns = patterns('reports_views.views',
# report patterns plain urls
url(r'^patterns/$', 'patterns'),
url(r'^patterns/create/(?P<alias>[a-z_]+)$', 'create_report_pattern'),
url(r'^patterns/edit/(?P<pattern_id>[0-9]+|)$', 'edit_report_pattern'),
TEMPLATE
acc_base.html
<li id="bt_patterns">
<a href="{% url 'reports_views.views.patterns' %}">
<span class="icon icon-sitemap"></span>{% trans "Templates" %}
</a>
</li>
TEMPLATE
reports_patterns.html
{% extends 'acc_base.html' %}
{% load staticfiles %}
{% load acctags %}
{% load i18n %}
{% block breadcrumb %}
<div class="left">
<ul class="breadcrumb">
<li>{% trans "Home" %}</li>
<li>{% trans "Reports" %}</li>
<li>{% trans "Report Templates" %}</li>
</ul>
</div>
FUNCTION
#login_required
#access_focus
def patterns(request):
data = dict()
data['patterns'] = report_registry.get_available_reports()
data['data'] = request.db_session.query(ReportPattern).order_by(ReportPattern.name)
return render_to_response('reports_patterns.html', data, context_instance=RequestContext(request))
Thank you for any help!
As you can see in docs for patterns, the first argument is the prefix for your imported views, so I assume you have a view named patterns that lives in report_views.views. And as you can see in url docs, there a optional param named name, used to call the url from a template with the {% url %} tag. You could try this:
...
url(r'^patterns/$', 'patterns', name='patterns_url'),
...
And the in your template:
<a href="{% url 'patterns_url' %}">