django: restrict url reachable only from one view - python

i'm new in Django.
I have a form once filled i keep the datas and redirect to another page, but i notice if i put the correct url i can access to this second page without passing by the first form.
here is my urls:
urlpatterns = patterns('',
url(r'newworkflow/$','access_mgmt.views.newworkflowform'),
url(r'newrole/$','access_mgmt.views.newrole'),
)
So if in my browser url i put /newrole i get the page but i want to have access to it only if the form on the first page "/newworkflow/ is filled.
Is it possible to restrict the access to the second page only if the first form page is filled?

Only by recording somewhere - say in the session - that the user has completed page1, and checking that in the view for page2.

Related

How to change content if the page is requested from a specific url in django?

Say I have two pages one is example.com/login and another page is example.com/admin
And when I put the credentials on the login page I get redirected to the admin page.
Admin page has a logout button. If I press that button then it redirects me to the login page again.
What I exactly want to do is, I want to display a message "Login again" dynamically (I know how to display a message dynamically) but only when user gets redirected from the login page via admin panel.
How can I do that?
You can do that either by:
Using Session:
upon logout you set a variable in the session, that this user has been loged out.
logout(request)
request.session['logged_out'] = True
Get parameter:
add a parameter to the redirected login url, if you find that parameter show you message, if there is no parameter you don't have to show.
redirect('login/?logged-out=True')
in both cases you have to check in your view, and add a a property to check with in your context.

Django add middleware that show form on each request

I am new to this django middleware I worked with django middleware by adding login page on each selected requested page. But now I want to show a html form as user request any page. So what should I use process_view or process_response to call the template (i.e html form).
add a form that takes input from user yes or no if its yes then go to the view page else show HttpResponseForbidden(). And next time if user had saved yes earlier then skip this form.

Use different wagtail page as root without slug

I'm attempting to set a page as the root home page of my wagtail based site.
I know you can set the homepage using the sites setting. But, my use case is different.
I'm using a "redirect" page which redirects to an index page as the homepage. The index is restricted to only allow ertain page types (since it's an index....). But, this causes issues with other pages not being in the tree if it was set at the root.
Hence, the redirect. But, upon redirecting the serve view of the redirect page to use this page it picks up the slug.
I still want it to appear at the root url. Meaning, /. How can I achieve this?
High level description:
from django.shortcuts import redirect
class IndexPage(Page):
# children restricted to IndexSubpage
class IndexSubpage(Page):
# parent restricted to IndexPage
class RedirectPage(Page):
def serve(request):
# Link is a link to a page through a ForeignKey....
return redirect(self.link, permanent=True)
So, this will pick up the slug of IndexPage instead of being at /

How to pass 'next' parameter to Django Auth logout_then_login view?

I am using Django 1.6, django.contrib.auth.views. I am using the login and logout url definitions below. Right now, when a user clicks my logout link, the 'logout_then_login' view gets triggered like it should, but when the user logs back in, the 'next' parameter is undefined and the page is forwarded to the default 'accounts/profile' path. What I would rather have happen is the logout_then_login view forward a 'next' value to the login view.
For example, the user is on page /foo/bar when they click logout. I would like them to be sent to the login screen with '/foo/bar' set as the next paramter, so when they login right away again, they are back on the same page they were at. I want it to be dynamic, based on the request.path when the logout link is clicked. I am aware of the static solution of overriding the default value for next when it is undefined.
Current urls.py:
from django.contrib.auth.views import login, logout_then_login
urlpatterns = patterns('base.views',
url(r'^accounts/login/$', login, name='login'),
url(r'^accounts/logout/$', logout_then_login, name='logout'),
)
I have tried using the extra_context argument for logout_then_login, but not even sure how it should look using a named url in a template:
Log Out
Am I on the right track - or is there a better way to do this?
Ok, so this turned out to be way easy. I was over thinking it when I was trying to cram the next parameter into the url template tag.
I simply needed to tack on the next parameter after the url template tag:
Log Out

django forms - reusing form request functions

I have a base.html which most of my pages inherit. Inside it defines a header which contains a search box. The code for the search box is some thing along the lines of....
if request.method == 'POST':
if 'search_button' in request.POST:
location = request.POST['location']
# Do something with location here and redirect to another page...
return HttpResponseRedirect('/lostandfound')
I have a couple of questions. First of all, how do I ensure that this code is sat in all my other views without rewriting it every time, this wouldn't be very DRY!!
Also how do I redirect to another page and pass the variable 'location' along with it? HttpResponseRedirect isn't the right way to do it i'm sure!
You should POST your search form to it's own search view, and display the results on that page.

Categories

Resources