Getting 'path' of previous URL from template - python

I know I can get the previous URL in the template by using the following:
{{ request.META.HTTP_REFERER}}
But I want to know if I there's a way to get only the path and not the absolute URL
(i.e /my-page instead of http://localhost:8000/my-page)
Like in the view we can do:
from urllib import parse
parse.urlparse(request.META.get('HTTP_REFERER')).path
Can I do something like that in the template as well?
Update (with more info): My usecase is to compare the previous url with another url within the same site to see if the user came in from there

Looks like there isn't a direct way to do that in the template, so this is what I ended up doing in the template to check if a particular (relative) url is part of the previous (absolute) URL :
{% url 'schoollist:add_school' as add_school_url %}
{% if add_school_url in request.META.HTTP_REFERER %}
Thanks for adding!
{% endif %}

A possible way to achieve this is to use urlparse to get components out of the referer URL, then get the relative path.
If you handle it with custom code this probably won't do much difference for basic cases but probably do some good with fancier edge cases:
from urlparse import urlparse
referer = request.META.get('HTTP_REFERER')
path = urlparse(referer).path
See: Parse URLs into components

Related

Can Django's URL tag show the URL of the RedirectView

Let's say that in my urls.py I have a url like this:
path("support/", RedirectView.as_view(url="http://www.example.com"), name="support"),
And in one of my templates I use the url tag:
{% url "support" %}
This of course outputs /support/ as expected. But what if I want it to output http://www.example.com instead? Is that at all possible? Skip the redirect basically.
So Link would output Link.
But what if I want it to render http://www.example.com instead? Is that at all possible? Skip the redirect basically.
No, in short it is not possible with django views, since the url is of another website and you can't render it in your own.
You can see what exactly render() does.
If you'd like to directly redirect, then simply use anchor tag as:
Visit website.
You can also do this dynamically, by creating a URLField in one of the models and simply iterating it with href attribute of anchor tag.

Rendering dynamic variable in a django template

I am trying to render dynamic Url in Django template as follows
<a href={{'jibambe_site_detail/'|add: site.id}}>{{ site }}</a>. This however is returning a TemplateSyntaxError at /jibambe_sites/
add requires 2 arguments, 1 provided. What am I missing or how should I render this dynamic URL, I want it to produce something like jibambe_site_detail/1
OP's strategy changed on this one but OP's original error seems to be because of the space after colon.
Use add:site.id instead of add: site.id
See a similar question about default here
From #schwobaseggl comment, I was able to add a dynamic url variable as follows <a href={% url 'site_details' pk=site.id %}>{{ site.name }}</a>
then in the urls.py urlpatterns, I gave the path to jibambe_site_details a name path('jibambe_site_detail/<slug:pk>', JibambeSitesDetails.as_view(), name='site_details'),

URL trouble to pass parameter

This link can not pass parameter to view.py
profile
It gives an error page not found, 127.0.0.1:8000/profile/edit/
There is not parameter there, even {{costumer.slug}} returns a string
Rest of template has no porblem to pass a parameter like this:
{{j.title}}
What can be wrong here?
Your problem is that you are missing a leading slash, so the browser is concatenating the URL with the one you're already on (you're on '/profile', you click 'edit', you go to '/profile/edit').
But you shouldn't be building up URLs like that. You should use the url tag. Assuming your URLconf is this:
url(r'^edit/(?P<slug>\w+)/$', 'profile.views.edit_profile', name='edit_profile')
you would do this in the template:
<a href="{% url 'edit_profile' slug=costumer.slug %}">

How to get the current URL of a page in a Django template?

I know there is another question with virtually the same title as mine but the solution in that one didn't work for me. My url is like this:
http://domain.com/videos/dvd/1/
If I use either {{baseurl}} or {{ request.get_full_path }} I get just this part:
http://domain.com/videos/
How can I get the entire url? I need to be able to do this from the template level.
EDIT
P.S. it should disregard any parameters that may be in the url.
You could get it in your view and pass it along into your template context so that it is available to you there.
https://docs.djangoproject.com/en/1.3/ref/request-response/#django.http.HttpRequest.build_absolute_uri
full_url = request.build_absolute_uri(None)
# pass full_url into the template context.

How to reverse django feed url?

I've been searching for hours to try and figure this out, and it seems like no one has ever put an example online - I've just created a Django 1.2 rss feed view object and attached it to a url. When I visit the url, everything works great, so I know my implementation of the feed class is OK.
The hitch is, I can't figure out how to link to the url in my template. I could just hard code it, but I would much rather use {% url %}
I've tried passing the full path like so:
{% url app_name.lib.feeds.LatestPosts blog_name=name %}
And I get nothing. I've been searching and it seems like everyone else has a solution so obvious it's not worth posting online. Have I just been up too long?
Here is the relevent url pattern:
from app.lib.feeds import LatestPosts
urlpatterns = patterns('app.blog.views',
(r'^rss/(?P<blog_name>[A-Za-z0-9]+)/$', LatestPosts()),
#snip...
)
Thanks for your help.
You can name your url pattern, which requires the use of the url helper function:
from django.conf.urls.defaults import url, patterns
urlpatterns = patterns('app.blog.views',
url(r'^rss/(?P<blog_name>[A-Za-z0-9]+)/$', LatestPosts(), name='latest-posts'),
#snip...
)
Then, you can simply use {% url latest-posts blog_name="myblog" %} in your template.

Categories

Resources