Unexpected URL concatenation - django

I have a url for product categories
url(r'^(?P<cat>[^/]+/$)', 'reviews.views.category'),
for example, if I am in the url: http://127.0.0.1:8000/ and click on "eletronics" in the menu, I go to http://127.0.0.1:8000/eletronics/
If I click again in other category (e.g. "books"), I go to http://127.0.0.1:8000/eletronics/books/
How I set up my url in way that I just get one (the last clicked) category in the url?

The menu links in your template may be pointing to "electronics/" and "books/" vs "/electronics" and "/books." To conform with DRY, I recommend naming your url patterns and using the {% url %} template tag to load the correct url route.
For example:
url(r'^(?P<cat>[^/]+/$)', 'reviews.views.category', name='category'),
And in your template:
{% url 'category' 'books' %}
Which will return the correct '/books' url route.

Related

How to replace re "." in Django template url tag

I have Django url routes that looks like this:
app_name = 'courses'
urlpatterns = [
url(r'./(?P<courseid>[0-9]+)/$', views.viewcourse, name='view_course'),
url(r'./(?P<courseid>[0-9]+)/review/$', views.reviewcourse, name='review_course')
]
The "." in the regular expression will usually be replaced by a slug, e.g.:
courses/computer-science/3/
Now I need a link in my 'view course' template to take the user to the review page, i.e:
courses/computer-science/3/review/
I could do this by simply appending review to the current url:
{{ request.path }}review
However, I would rather do it the 'correct' way using a template url tag
{% url 'courses:review_course' courseid=course.pk %}
However, this produces:
courses/30/review, which of course fails.
Is it possible to generate the correct link using the {% url %} tag without having to change the "." into a name capture parameter?
Change your urls.py :
url(r'(?P<course_type>[a-z_-]+)/(?P<courseid>[0-9]+)/$', views.viewcourse, name='view_course')
Now in your view you have access to course_type in your kwargs and you can then load whatever you want (as I have no code, i'm guessing what you're doing :)).
And in your template:
{% url 'courses:review_course' courseid=course.pk course_type=something%}

Django: correct URL appears in developer tools but link doesn't use that src

I've created a link in a Django template which appears to lead to the correct URL in the elements pane of the developer tools. But the actual link in the page only goes to localhost. I thought I understood how the Django url template tag worked, but I must be missing something.
URL pattern:
url(r'^dollhouse/(?P<dollhouse>[0-9]+)$', views.dollhouse, name='dollhouse')
Template:
{% for workingdollhouse in dollhouses %}
<a href src="{% url 'dollhouse' dollhouse=workingdollhouse.id %}">{{workingdollhouse.dollhouse_name}}</a>
{% endfor %}
Text of element appearing in developer console (hovering over the '"/dollhouse/1"' reveals the intended address of localhost/dollhouse/1):
'<a href src="/dollhouse/1">dollhouse1</a>'
Actual link just goes to localhost.
This has nothing to do with Django.
a elements don't have a src attribute. The destination goes in the href attribute, which you left blank.
{{workingdollhouse.dollhouse_name}}

Is it possible to display a specific block from a template in django?

Just like in html when you reference a section of a page with for instanceVisit the Useful Tips Section
Would there be a way to do a similar thing in django if for instance I wanted to load my page straight to the tips section? I am extending base.html to my home page that has a tips section. Right now i have a static url home i want to do the exact same but with djangos dynamic url something like {% url 'home'/#tips %}
You can just add the fragment identifier right after the URL returned by the {% url %} template tag:
home

Showing 'cancel' on login page to return user to where they were (using django.contrib.auth)

We are using the #login_required decorator so that users see a login page if they try to access a url for which they need to be authenticated.
We want to show a 'cancel' button on the login page, which should return the user to whichever page they were on when they tried to access the url (by clicking a link etc - we don't need to deal with them manually entering the url).
At the moment our login.html looks for a request parameter 'login_cancel_url' and if present uses that (otherwise the home page).
However, this means we have to manually pass this parameter (set to the url of the current page) whenever we show a link or button that leads to an 'authentication required' url.
Is there a more elegant way to do this?
Thanks, Martin
Well you can try get the referrer header from the request but as far as I am aware, it's browser dependent and is not very reliable so the way you are doing it is probably best. You could try make life easier by creating template tags to avoid having to rewrite the return URL manually.
You are easily able to get the current URL from django's request object on any page, so instead of setting it manually on the link, you could write a snippet of html:
link_to_login.html
<!-- You should probably get /login/ using the {% url ... %} template tag -->
<a href="/login/?login_cancel_url={{ request.path|urlencode }}">
Login Page</a>
and use the {% include "link_to_login.html"%} template tag.
Alternatively, If the text needs to be different depending on the link you can instead create an inclusion template tag:
templatetags/extra_auth_tags.py
#register.inclusion_tag('templates/extra_auth_tags/login_link.html')
def login_link(context, text=None):
return {
'text':text
}
templates/extra_auth_tags/login_link.html
<!-- You should probably get /login/ using the {% url ... %} template tag -->
<a href="/login/?login_cancel_url={{ request.path|urlencode }}">
{% if text %}
{{ text }}
{% else %}
Some Default Text
{% endif %}
</a>
and then call it in your templates as {% login_link text="Check you messages" %}. Be aware that keyword arguments for inclusion tags are only supported in the django dev version so you might need to write the template tag by hand.

New url syntax from django 1.3/dev onwards

Why did the django core developers allow the url templatetag to point directly to a django view function? (reference - https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url)
{% load url from future %}
{# 1st method: pointing to a view function #}
{% url 'app_views.client' %}
{# 2nd method: pointing to a named url #}
{% url 'myapp:view-name' %}
One can already name the url in urls.py and hence use the 2nd method to point to a specific url. It doesn't feel right to allow developers to actually reference a view function directly from the template.
Does anyone know why this decision was made?
Passing a dotted view function name to the {% url %} template tag is simply the form the template tag took in the earlier days of Django, before you could name URLs. It's still supported, though as you point out, you probably wouldn't use it in a modern application.
URLs in Django are just mappings to views. Therefore, in the template, using a named URL is just indirectly referencing the view anyway.
The exception is where a single view is mapped to by multiple URLs.
Also note that they are planning to change the syntax of the url tag in 1.5. It will take a context variable as the parameter, rather than a string. It will still take views or named URLs though.