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 %}
Related
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 }}
In my django project I have two options that use querystrings to define a kind of list being fetched, e.g:
Shopping
Chores
On top of that I also want to check which list has been selected by the user and make it appear bold in the UI. So
{% if 'shopping' in request.GET.list or not request.GET.list %}
<b>Shopping</b>
Chores
{% elif 'chores' in request.GET.list %}
Shopping
<b>Chores</b>
{% endif %}
What's really confusing me now, is in addition to Shopping and Chores I also want to have two sub-options that define the order of the list. New and Old, for instance. To me it seems like the only way of doing this is with another duplication of all the code.
{% if 'new' in request.GET.list %}
{% if 'shopping' in request.GET.list or not request.GET.list %}
<b>Shopping</b>
Chores
<b>New</b>
Old
{% elif 'chores' in request.GET.list %}
Shopping
<b>Chores</b>
<b>New</b>
Old
{% endif %}
{% elif 'old' in request.GET.list %}
{# ... #}
{% endif %}
I think you can already see how insane this becomes to manage and you would still need to do the same for the Old if statement. I really don't know what to do here because I can't see any other way to (1) Know what should be bold and not bold. And (2) Known whether each option should begin with a ? or &.
Regarding your first problem with having bold selected value, by the use a of a HTML class you can do the following instead for example.
In your css file (or style block in your html file):
.selected {font-weight: bold;}
So your html can now become somehing like,
<a class="{% if 'shopping' in request.GET.list or not request.GET.list %} selected{% endif %}" href="{% url 'index' %}?list=shopping">Shopping</a>
<a class="{% if 'chores' in request.GET.list %}selected{% endif %}" href="{% url 'index' %}?list=chores">Chores</a>
This way you dont have to write your html twice or more for each case.
As for your second issue, you can do something like below if you are using your 'new' and 'old' in your urls or html,
{% with 'new old bla' as list %}
{% for option in list.split %}
<a class="{% if 'shopping' in request.GET.list or not request.GET.list %} selected{% endif %}" href="{% url 'index' %}?list=shopping&option={{ option }}">Shopping</a>
<a class="{% if 'chores' in request.GET.list %}selected{% endif %}" href="{% url 'index' %}?list=chores&option={{ option }}">Chores</a>
{% endfor %}
{% endwith %}
Thats just an example of how you would use it, but this will save alot of code writing.
Hope this helps!
You could use a QueryDict for your query strings. That's what Django uses internally.
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.QueryDict
But really, I would consider refactoring your code. Put routing logic in your urls.py and business logic in your view functions. Try to keep the template files as uncomplicated as possible.
Instead of /?list=shopping you can use a regular url: /list/shopping/, for example.
{% if shelf.script or shelf.log_url %}
{% if shelf.log_url %}
Log URL: {{ shelf.log_url }}<br />
{% endif %}
{% if shelf.log_url %}
<b>Another URL:</b> {{ shelf.log_url.replace("/tmp/BOOTLOG/", "http://www.sxp.com") }}<br />
{% endif %}
{% endif %}
I have already got a log URL, I would like to update this URL with a domain address and adding /index.html to at the end.
I would like to replace this URL
shelf.log_url output = /tmp/BOOTLOG/darwin_12345.tgz
to something like this.
www.sxp.com/darwin_12345/index.html
How can I do in Django template ?
What you are trying to do can be achieved by a custom template tag. You can find more about custom template tags at:
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/
you should try using custom filter for this. and you will get better idea from Django custom filter documentation
for example
{{shelf.log_url|replace:"url you need"}}
add filter in your any file for example filters.py
#register.filter(name='replace')
def replace(url, new_url):
line = url.replace('/tmp/BOOTLOG/', new_url)
return line
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']
I want to be able to do an if tag based on the current URL value.
for example, if the current page's url is accounts/login/ then don't show a link, without passing a variable from the view.
I am not sure how I can write an {% if %} tag for this, is it possible?
You can also do this for dynamic urls using:
{% url 'show_user_page' user=user as the_url %}
{% if request.get_full_path == the_url %}something{% endif %}
where your urls.py contains something like:
(r'^myapp/user/(?P<user>\d+)/$', 'show_user_page'),
I know this because I just spent ages drafting a stackoverflow question, when I found the answer in the docs.
I'd say even in simple cases this might be the better approach, because it is more loosely coupled.
If you pass the "request" object to your template, then you are able to use this:
{% if request.get_full_path == "/account/login/" %}
For dynamic urls, you can also use the request.resolver_match attribute (docs):
HttpRequest.resolver_match
An instance of ResolverMatch representing the resolved URL. This attribute is only set after URL resolving took place, which means it’s available in all views but not in middleware which are executed before URL resolving takes place (you can use it in process_view() though).
The returned ResolverMatch object has many useful attributes, such as the view_name attribute, which returns the same name you would have passed to the url templatetag to generate the current url.
view_name
The name of the view that matches the URL, including the namespace if there is one.
See the docs for this and other attributes.
Applying this to the example from #nimasmi's answer, you would get:
{% if request.resolver_match.view_name == 'show_user_page' %}something{% endif %}
where your urls.py contains something like:
(r'^myapp/user/(?P<user>\d+)/$', 'show_user_page'),
Note that when you use URL namespaces, view_name will return the namespace qualified url/view name, e.g. app:urlname.
Compared to the answer by #nimasmi, this simplifies the template code a bit, by not needing the separate {% url %} tag to generate the url to compare with. This is especially true when you do not need to compare view parameters, just the view name. If you do need to compare parameters in the url, you can easily use the ResolverMatch.args and kwargs attributes, though.
Maybe like this?
if "order" in request.path
Using "in" allows you to match URLs like:
customers, customer, customer/new, customer/edit, etc
<ul class="nav nav-pills nav-fill">
<li class="nav-item">
<a class="nav-link
<pre>{% if "order" in request.path %} active {% endif %} "</pre>
href="/orders">Order List</a>
</li>
<li class="nav-item">
<a class="nav-link
<pre>{% if "customer" in request.path %} active {% endif %} "</pre>
href="/customers">Customer List</a>
</li>
<li class="nav-item">
<a class="nav-link
<pre>{% if "product" in request.path %} active {% endif %} "</pre>
href="/products">Product List</a>
</li>
</ul>