Comparing Two Urls Using Reverse And HTTP_REFERER - django

I am wanting to prevent a user from accessing a certain page unless they have been redirected. In order to do this I thought id do this:
if(request.META.get('HTTP_REFERER') != reverse('some_page')):
return redirect('some_page')
This works almost perfectly except that request.META.get('HTTP_REFERER') returns the whole url address, while reverse('some_page') returns the abbreviated url.
For example
request.META.get('HTTP_REFERER') returnshttp://127.0.0.1:8000/page_one/some_page/
reverse('some_page') returns/page_one/some_page/
How can I either add the (sorry but I dont know the correct term) first part of the url (http://127.0.0.1:8000) to reverse('some_page'), or remove it from request.META.get('HTTP_REFERER') so they can be both compared in the if statement please?
Thank you.

This works :)
if(request.META.get('HTTP_REFERER') != request.build_absolute_uri(reverse('some_page'))):
return redirect('some_page')

Related

How to append string in the end of Django redirect in view?

I have a product on my platform and when a specific user does an action, I want to redirect them to example.com/product/product-slug/#SectionInWebsite.
However, I am not able to find a way to append "#SectionInWebsite" to the end of the redirect function.
return redirect('ProductSingle', product.slug, "#SectionInWebsite")
This worked for me (I had to use different names on my machine, but it should work):
# views.py
return redirect('{}#sectionInWebsite'.format(reverse('ProductSingle', kwargs={'product_slug':product.slug})))
That is, assuming your urls.py has something like this:
# urls.py
...
path('ProductSingle/<str:product_slug>', views.ProductSingle, name='ProductSingle'),
...
This is just a variation of the answer provided here, applied to your situation.
do this :
return redirect(reverse('ProductSingle',product.slug) + '#SectionInWebsite')
Maybe this can help you, using reverse.
return redirect(reverse('ProductSingle', product.slug) + '#SectionInWebsite')

how to request last segment of url in Flask

I feel like the answer is simple, yet I can't seem to figure it out. I have a URL:
http://127.0.0.1:5000/fight_card/fight/5
And I'm trying to use just the "5" in my code as that is the ID for the current fight in the SQL table. So far, I've tried
fight = Fight.query.filter_by(id=request.path).first()
However that returns:
fight_card/fight/5
Is there any way I can use "request" to target just the 5? Thank you in advance!
You should include a variable, current_fight_id, in your route definition. You can then access that variable in your view function.
#app.route('/fight_card/fight/<current_fight_id>')
def fight_route(current_fight_id):
print(current_fight_id) # use variable in route
Alternatively, you could use the approach you're using but modify the string that's returned. If you have a string:
endpoint = "fight_card/fight/5" # returned by your current code
You can access the five (current_fight_id) with:
current_fight_id = endpoint.split("/")[-1] # grab the segment after the last "/"
request.path would give you: /fight_card/fight/5. Then, you can split('/') to get a list of the parts.

How to use variable at start of django url to return to view?

I am trying to pass the first part of a django url to a view, so I can filter my results by the term in the url.
Looking at the documentation, it seems quite straightforward.
However, I have the following urls.py
url('<colcat>/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('<colcat>/', views.collection_view, name='collection_view'),
In this case, I want to be able to go to /living and have living be passed to my view so that I can use it to filter by.
When trying this however, no matter what url I put it isn't being matched, and I get an error saying the address I put in could not be matched to any urls.
What am I missing?
<colcat> is not a valid regex. You need to use the same format as you have for name.
url('(?P<colcat>[\w\-]+)/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('(?P<colcat>[\w\-]+)/$', views.collection_view, name='collection_view'),
Alternatively, use the new path form which will be much simpler:
path('<str:colcat>/collection/<str:name>', views.collection_detail, name='collection_detail'),
path('<str:colcat>/', views.collection_view, name='collection_view'),

django parameter gets none even on redirect with parameter

I have the following view that I want to only stay in the current view if it hasn't been redirected.
def surveys_view(request, survey=None):
if survey != 'survey1':
return redirect('matching:surveys', survey='survey1')
My urls.py looks like this:
url(r'^surveys', views.surveys_view, name='surveys'),
url(r'^surveys/(?P<survey>survey1|survey2)', views.surveys_view, name='surveys'),
The problem is that the survey variable is None every time causing infinite redirects. I would think that after the redirect, the survey value would be set to 'survey1'.
How can I have the survey parameter set to 'survey1' instead of None?
in django urls list is parsed from 0th position, so in your urls,
url(r'^surveys', views.surveys_view, name='surveys')
has first priority, since the regular expression matches like any uri starting with surveys<whatever>, so either change the url settings to,
url(r'^surveys/$', views.surveys_view, name='survey-list'),
url(r'^surveys/(?P<survey>survey1|survey2)', views.surveys_view, name='survey-details'),
or change the order
url(r'^surveys/(?P<survey>survey1|survey2)', views.surveys_view, name='survey-detail'),
url(r'^surveys', views.surveys_view, name='survey-list'),
NOTE: notice there is also change in name parameter, since it is better to be set as a unique value

Django, Pass QuerySet to url using redirect/redirect_to

How can I redirect from view to another url, passing my queryset to another view?
I tried this:
return simple.redirect_to(request, 'some_url', **{'queryset': results})
and this
return redirect('some_url', queryset=results )
but it does not work....
How can i do it?
Gabi.
How are you expecting this to work? Redirection happens by getting the browser to request another URL. Anything you want to pass as a parameter to the redirection must therefore go into the URL you're redirecting to. It simply doesn't make sense to put a queryset into a URL parameter.
Presumably you could pass whatever arguments you used to get the queryset in the first place, but that's a lot of extra work.
Do you really need to redirect at all? What about simply calling the new view from your original one, and returning its response?