I am trying to setup a form so that the page the form was submitted from is captured as well as used to redirect the visitor back to that page. The form appears on every page of the site and is included on the base.html template. This is a Django 1.7 project.
I am open to using a different approach than what I have so if there is a better way please suggest.
In settings.py
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)
In views.py.
def submit_user_content(request):
path = request.GET.return_url
user = request.user
text_content = request.POST['user_content']
UserContent(url = path, author = user, content = text_content).save()
return redirect( return_url, context_instance=RequestContext(request, processors=[user_content]))
In base.html
<form id="submit_user_content" method="post" action="/myapp/submit_user_content/?return_url={{ request.path|urlencode }}">
{% csrf_token %}
<textarea name="user_content" form="submit_user_content" rows="8" cols="80"></textarea>
<input type="submit" value="Submit" />
</form>
In the browser when you submit the form, it goes to:
http://127.0.0.1:8000/myapp/submit_user_content/?return_url=/myapp/defin/
And produces this error:
AttributeError at /myapp/submit_user_content/
'QueryDict' object has no attribute 'return_url'
Although in the traceback I see the request is populated with the following:
'<WSGIRequest\npath:/myapp/submit_user_content/,\nGET:<QueryDict: {u\'return_url\': [u\'/myapp/defin/\']}>'
What is the correct way to pick up return_url? Or is there a better way to redirect the form back to the page the user was on and capture that previous page path?
They way you retrieve the GET
parameter is slightly off.
Try
request.GET.get('return_url')
or
request.GET['return_url']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With