request.resolver_match.kwargs only works while inside for loop? - python

I'm using request.resolver_match.kwargs with django to grab the url param for a view I'm trying to link to. When I use it as so
Print this page
I get a noreversematch error. however if I put it inside a for loop it works as so
{% for uid in request.resolver_match.kwargs.uid %}
Print this page
{% endfor %}
With the obvious side effect of having as many links as there are characters in the url param. I'm not sure why it works in the for loop but not as a standalone.

Related

Link to a page in django cms, first check if it exists

I often do things like this in a django template, with django-cms:
{% load cms_tags %}
Imprint
On production, this fails silently, and the href attribute is empty. On development, I'm forced to insert the page with id "imprint", otherwise I get a "DoesNotExist" exception.
How can I improve this situation? Maybe I'm looking for something like
{% if 'imprint'|cms_page_exists %}
...the link and stuff...
Is there a known best practice for this (not quite seldom) use case? Or do you all use it as shown first?
You could assign a tag result to a variable and then check is it empty:
{% page_url 'imprint' as url %}
{% if url %}
Imprint
{% endif %}
Other ways imply creating your own template tag or filters, so the above is the simplest one IMHO.
See also an example in the docs.

Django - Passing argument through href for URL dispatcher?

So I am working on a website where at some point I do a search and list a list of textbooks on the page. From there I want the user to be able to click on a textbook and each textbook will have it's own details page. I have been trying to work with Django's URL dispatcher but I am having difficulties. Code and description below.
results.html
<table class="table">
{% for items in results %}
<tr><td>{{items.textbook_name}}</td><td>{{items.class_name}}</td><td>{{items.author}}</td><td>{{items.isbn}}</td><td>></td></tr>
{% endfor %}
</table>
Views.py
def textbook(request, text_name):
return render_to_response(
'textchange/textbook.html',
locals(),
context_instance=RequestContext(request)
)
Urls.py
url(r'^results/(?P<text_name>\w+)/$', views.textbook, name="textbook"),
From my understanding I thought I was passing the items.textbook_name as text_name to urls from the html and then views would be called with text_name as an argument but it is not working. I might be a little backwards here. I need the textbook_name from the textbook the user clicks on so on the details page I can display all it's information from the database.
Thanks.
Quick explanation.
Let's say that your url doesn't need text_name parameter, so that:
{% url 'textchange:textbook' %}?text_name={{items.textbook_name}}
will result in:
/results/?text_name=some-name
text_name won't get passed to url tag, it was simply glued on the end of url.
To pass text_name into url, and build proper url, you should do that:
{% url 'textchange:textbook' text_name=items.textbook_name %}
and it will result in url:
/results/some-name/

linking outside of Django site in dev mode

I'm testing a django site on the local server: http://127.0.0.1:8000/
The sites internal links all work great, even the static links. However, when I try to link to an outside link with say google as text inside a text field of my blog model it doesnt render the link correctly. That blog model is then passed through as |safe (so that the html is rendered) to the template, and the link is instead trying to append everything to the static root:
http://127.0.0.1:8000/blog/view/http://google.com
Anyone know how to keep my static links working, but still have links that go outside of the site?
EDIT:
For example, here is a blog post that is stored in a TextField() from the admin, inside my blog app. The blog post has some links. The link to the /static/mytextfile works fine as it appends that to the http://127.0.0.1:8000/. However, the github link isnt working as it attempts to append the github link to http://127.0.0.1:8000/ and thus the outputted html creates "http://127.0.0.1:8000/http://github.com/":
<p><b>The Code</b><br>
<a href=”http://github.com/”>GitHub</a>
<p><b>Example Outputs</b>
<br>a text file
Here's the 404 error that I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/view/%E2%80%9Dhttp://github.com/%E2%80%9D
EDIT 2:
This is how I have been 'escaping' the html filter. Up until now it has worked fine at leaving my <p> etc alone. It is not however leaving my href links alone!
{% autoescape off %}
{% block content %}
<p>{{ post.body|safe }}</p>
{% endblock %}
{% endautoescape %}
Something seems to be wrong with your double quote characters for the GitHub link.
Instead of:
<p><b>The Code</b><br>
<a href=”http://github.com/”>GitHub</a>
Try:
<p><b>The Code</b><br>
GitHub
I think you should try google.

Using Django template tag "with" with the result of another template tag

I have a comment_form.html template, which is used in multiple places in my app, and I'd like to be able to pass the endpoint's url into that template from a parent template. Normally I would do this using the with tag:
{% with endpoint='/comments' %}
{% include 'comment_form.html' %}
{% endwith %}
The problem is that I can't use a string literal '/comments' here, but instead I need a url tag, like so: {% url 'blog:comments:comments' username=post.user.username object_id=post.id %}. The with template tag seems to expects a literal or a context variable and doesn't seem to be able to comprehend "use the result of another template tag".
One solution would be to pass the strings 'blog:comments:comments', post.user.username, post.id all separately. But this is a problem because different uses of the comment form may require different arguments to uniquely define the endpoint.
How can I use with with the result of another template tag?
You can't, but you don't need to. The url tag has an alternative syntax that injects is result into the context:
{% url 'blog:comments:comments' username=post.user.username object_id=post.id as endpoint %}

Django template check for empty when I have an if inside a for

I have the following code in my template:
{% for req in user.requests_made_set.all %}
{% if not req.is_published %}
{{ req }}
{% endif %}
{% empty %}
No requests
{% endfor %}
If there are some requests but none has the is_published = True then how could I output a message (like "No requests") ?? I'd only like to use Django templates and not do it in my view!
Thanks
Even if this might be possible to achieve in the template, I (and probably many other people) would advise against it. To achieve this, you basically need to find out whether there are any objects in the database matching some criteria. That is certainly not something that belongs into a template.
Templates are intended to be used to define how stuff is displayed. The task you're solving is determining what stuff to display. This definitely belongs in a view and not a template.
If you want to avoid placing it in a view just because you want the information to appear on each page, regardless of the view, consider using a context processor which would add the required information to your template context automatically, or writing a template tag that would solve this for you.

Categories

Resources