105
You can add a next
field to your form, and set it to request.path
. After you processed your form you can redirect to the value of this path.
template.html
<form method="POST">
{% csrf_token %}
{{ form }}
<input type="hidden" name="next" value="{{ request.path }}">
<button type="submit">Let's Go</button>
</form>
views.py
next = request.POST.get('next', '/')
return HttpResponseRedirect(next)
This is roughly what django.contrib.auth
does for the login form if I remember well.
If you pass through an intermediate page, you can pass the βnextβ value via the querystring:
some_page.html
<a href="{% url 'your_form_view' %}?next={{ request.path|urlencode }}">Go to my form!</a>
template.html
<form method="POST">
{% csrf_token %}
{{ form }}
<input type="hidden" name="next" value="{{ request.GET.next }}">
<button type="submit">Let's Go</button>
</form>
57
You can use the HTTP_REFERER
value:
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
Note that this will not work if the client disabled sending referrer information (for example, using a private/incognito browser Window). In such a case it will redirect to /
.
- [Django]-What's the difference between `from django.conf import settings` and `import settings` in a Django project
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
13
You can use this
return redirect(request.META.get('HTTP_REFERER'))
Make sure to import this
from django.shortcuts import redirect
- [Django]-Django REST Framework custom fields validation
- [Django]-Django delete superuser
- [Django]-Django models: Only permit one entry in a model?
3
My favorite way to do that is giving the request.path as GET parameter to the form.
It will pass it when posting until you redirect.
In Class-Based-Views (FormView, UpdateView, DeleteView or CreateView) you can directly use it as success_url.
Somewhere i read that itβs bad practise to mix GET and POST but the simplicity of this makes it to an exception for me.
Example urls.py:
urlpatterns = [
path('', HomeView.as_view(), name='home'),
path('user/update/', UserUpdateView.as_view(), name='user_update'),
]
Link to the form inside of the template:
<a href="{% url 'user_update' %}?next={{ request.path }}">Update User</a>
Class-Based-View:
class UserUpdateView(UpdateView):
...
def get_success_url(self):
return self.request.GET.get('next', reverse('home'))
In your function based view you can use it as follows:
def createadv(request):
uw = getuw(request.user.username)
if request.method =='POST':
form = AdverForm(request.POST, request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()
next = request.GET.get('next', reverse('home'))
return HttpResponseRedirect(next)
args = {}
args.update(csrf(request))
args['username'] = request.user.username
args['form'] = AdverForm()
args['uw'] = uw
return render_to_response('createadv.html', args)
- [Django]-Django-Admin: CharField as TextArea
- [Django]-In a Django QuerySet, how to filter for "not exists" in a many-to-one relationship
- [Django]-Django upgrading to 1.9 error "AppRegistryNotReady: Apps aren't loaded yet."
3
you could do this easily with a simple one-liner JS
<button onclick="history.back()">Go Back</button>
This will take you back to the previous page of your history list.
If you donβt have a history
https://www.w3schools.com/jsref/met_his_back.asp
- [Django]-How to force application version on AWS Elastic Beanstalk
- [Django]-Cron and virtualenv
- [Django]-How to send a correct authorization header for basic authentication
2
Use HTTP_REFERER
value:
-
for use in func
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
-
for use in template
<a href="{{request.META.HTTP_REFERER}}">Go Back</a>
- [Django]-How to run this code in django template
- [Django]-How can one use enums as a choice field in a Django model?
- [Django]-What's the difference between ContentType and MimeType?
0
In case this helps someone I got this to work in class based UpdateView
template
<form class="form" method="POST">
{% csrf_token %}
<!-- hidden form field -->
<input type="hidden" id="previous_page" name="previous_page"
value="/previous/page/url">
<!-- any other form fields -->
{{ form.name|as_crispy_field }}
{{ form.address|as_crispy_field }}
<!-- form submit button -->
<button class="btn btn-primary" type="submit" id="submit">Submit</button>
</form>
<!-- JS to insert previous page url in hidden input field -->
<script>
prev = document.getElementById("previous_page");
prev.value = document.referrer;
</script>
views.py
class ArticleUpdateView(generic.UpdateView):
model = Article
form_class = ArticleForm
template_name = 'repo/article_form.html'
def form_valid(self, form):
form.instance.author = self.request.user
# if form is valid get url of previous page from hidden input field
# and assign to success url
self.success_url = self.request.POST.get('previous_page')
return super().form_valid(form)
The view now redirects you back to the page where you had clicked the βUpdate/Editβ button. Any URL query parameters are also preserved.
- [Django]-Django β How to rename a model field using South?
- [Django]-How do I access the child classes of an object in django without knowing the name of the child class?
- [Django]-How to create a custom decorator in Django?