I am building a simple blog using Web2Py on Debian Linux.
I have a controller called blog.py, to which I added the following function, along with an if block:
def display_form():
form = SQLFORM(db.blog)
if form.process().accepted:
session.flash = 'form accepted'
redirect(URL('thanks'))
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return locals()
I proceeded to add a "view" html file called blog/display_form.html, with a basic template, as follows:
{{extend 'layout.html'}}
<h1>Display Form</h1>
{{=form}}
I load the "display_form" blog page just fine, and it accepts all the input successfully, but it does not redirect to a thank you page. Instead, the browser generates an "invalid function blog/thanks" error.
I tried removing the compiled app via the Web2Py admin interface, and recompiled everything. Still does not work. I added a "view" for the "Thanks" page, but that does not change anything. I restarted the Web2Py framework and the web server, but still no go.
Some web sites refer to a possible routes.py issue, but I am confused as to why that would be pertinent at all.
Please help,
I am hitting a brick wall here.
So, after tweaking a number of things, and removing all of the compiled files, and starting from scratch again, the solution turned out to be way more simple than I was trying to make it.
I simply defined a function called thanks in the aforementioned blog.py controller, and returned the local variables, like so:
def thanks():
return locals()
I then added a blog/thanks view file, with a basic html header, stating:
Thank you for submitting the form!
And it finally redirected the display_form blog page as intended to a thanks page, thereby flashing the form accepted message (also as expected).
Thanks for your help, Anthony!
Cheers.
When you pass only a single argument to the URL() helper, it assumes that argument is the function name and that the controller is the current controller. Since the current controller is blog, URL('thanks') will generate the following URL: /yourapp/blog/thanks.
Presumably, your thanks function is in a different controller (perhaps default.py), in which case, a request for the above URL will generate the error you observe. Just change the URL to URL('default', 'thanks').
Related
I am new to Django, and having a difficulty soting out a problem. My code is processing extra method in views.py even without being called.Below is an example:-
When I am cliking on Home button on my page.
Landing Page:
I am being redirected to correct view method i.e., index.
IndexMthod in views.py:
After loading the page my code is hitting another method edit content, without being called with a param favicon.io
Concern Method
which is my concern.
Though it is not affecting my django app, but still it's annoying to see extra method being called everytime.
Please let me know, if I have done something wrong here or if any more info is required from my end. Thanks in advance.
I'm using request.session['url'] to store URL and use it to go back to a previous page in a couple of views in my django app. It works really well, except in one case... right after login, the request.session['url'] is empty as it gets only populated when using some views.
so, if I go directly to a view that expect to find something in request.session['url'], it crash.
Is there a way or a place to set a request.session['url'] = 'home' as soon as the user logs in?
Thanks
How about using this instead:
default_url = '/home'
request.session.get('url', default_url)
It gives you a default value if there is none in the session.
I am using django.views.i18n.set_language() redirect view and HTML form where user can choose language.
I am doing everything as it's described in Django documentation for i18n translation .
The only difference that I made is that within HTML form I changed value of next parameter from {{redirect_to}} to {{request.get_full_path}}
Anyway, It worked completely fine while I was testing it locally. I could select different language and it would reload current page but with different language.
Now I put application on VPS where I use Gunicorn as application server and Nginx as web server. Now when I select different language it still changes it but it always redirect me to to home page / (site root).
I have no idea why is that happening now and how to change it. I want that he reloads the same page again instead of redirecting me to the / always. Anyway, at translation still works fine.
Thank you for your replys
Kind regards
Wander Nauta answered it in the comments
Are you sure request.get_full_path is available in the template?
You need to add django.core.context_processors.request in your template context processors settings, which is not there by default.
Is there anyway to pass context variables to a redirect response? I want to redirect a user to a success page after they submit a form, but I don't want the success page to be just a static html file. I need to display extra information based on the form data.
I have looked at this question, but the solution presented there simply renders a different file at the same url. I'd like to redirect the user so that hitting refresh at the page won't submit duplicate entries into the application.
Right now the only thing I have been able to use with some success is redirecting to a url while passing it GET variables as described here. That just seems like a bit of a hack, and was just wondering if there is any better solution...
Thank You
The way I see it you have three options:
Use GET variables in the redirect.
Store something in the session.
If you are creating an object using the form that was submitted, put the id of that object in the redirect url and use it in the new view.
The limitation you are running up against is that http is stateless, not something inherent in django.
How about storing your values in a session, then have the redirected page pick up the values from there?
Does anyone have any simple code samples for Django + SWFUpload? I have it working perfectly in my PHP application but Django is giving me headaches.
Unfortunately I can't give you any very detailed code samples, but I have quite a bit of experience with working with SWFUpload + Django (for a photo sharing site I work on). Anyway, here are a few pointers that will hopefully help you on your quest for DjSWF happiness :)
You'll want to use the cookies plugin (if of course you are using some sort of session-based authentication [like django.contrib.auth, and care who uploaded what).
The cookies plugin sends the data from cookies as POST, so you'll have to find some way of getting this back into request.COOKIES (process_request middleware that looks for a settings.SESSION_COOKIE_NAME in request.POST on specific URLs and dumps it into request.COOKIES works nicely for this :)
Also, remember that you must return something in the response body for SWFUpload to recognize it as a successful upload attempt. I believe this has changed in the latest beta of SWFUpload, but anyway it's advisable just to stick something in there like 'ok'. For failures, make use of something like HttpResponseBadRequest or the like.
Lastly, in case you're having trouble finding them, the uploaded file is in request.FILES :)
If you have anything perplexing I haven't covered, feel free to post something more detailed and I'll be happy to help.
Django version of the samples for SWFUpload:
http://github.com/naltimari/django-swfupload-samples/tree/master
So long uploadify. Great idea but it is just buggy, especially on Windows.
The following is my Django-specific implementation for fixing this issue (i.e. my uploads were failing in Firefox with a 302 Redirect).
In my initial view that generates the page with the uploader on it, I looked at the cookies and found sessionid
ipdb> self.request.COOKIES
{'csrftoken': '43535f552b7c94563ada784f4d469acf', 'sessionid': 'rii380947wteuevuus0i5nbvpc6qq7i1'}
When I looked at what was being posted in the SWFUploadMiddleware (when using Firefox), I found that the sessionid was not being set.
In my intial view that generates the page that contains the upload handler, I added the sessionid to the context.
context['sessionid'] = self.request.session.session_key
In my swfuploader settings, I added sessionid to the post-params option as follows:
post_params: {... "sessionid": "{{ sessionid }}" ...},
Now, when I looked in the SWFUploadMiddleware, I could see the sessionid being posted, and my uploads started working if Firefox.
ipdb> request.POST
<QueryDict: {... u'session_id': [u'rii380947wteuevuus0i5nbvpc6qq7i1'],...}>
For completeness, my SWFUploadMiddleware looks like this...
from django.conf import settings
from django.core.urlresolvers import reverse
class SWFUploadMiddleware(object):
def process_request(self, request):
if (request.method == 'POST') and (request.path == reverse('upload_handler')) and request.POST.has_key(settings.SESSION_COOKIE_NAME):
request.COOKIES[settings.SESSION_COOKIE_NAME] = request.POST[settings.SESSION_COOKIE_NAME]
# http://stackoverflow.com/questions/6634666/403-forbidden-error-on-swfupload-and-django
# Fix for problem uploading images (403 error) in Firefox 20 and others
if request.POST.has_key('csrftoken'):
request.COOKIES['csrftoken'] = request.POST['csrftoken']