我试图在用户输入后从我的views.py重定向到另一个URL - 视图实际上发生了变化,但不幸的是浏览器的URL保持不变。
URLs.朋友:
path('start/', StartProject.as_view(), name='start_url'),
path('settings_1/', settings_1_view, name='settings_1_url'),
path('settings_2/', settings_2_view, name='settings_2_url'),
path('settings_3/', settings_3_view, name='settings_3_url'),
我的views.py:这里我尝试在选择后将用户重定向到另一个页面。
views.朋友
class StartProject(TemplateView):
template = "start.html"
def get(self, request):
class_form = ClassChoiceForm(request.POST or None, initial={'class_select': 1})
context = {
'class_form': class_form
}
return render(request, self.template, context)
def post(self, request):
class_form = ClassChoiceForm(request.POST)
if class_form.is_valid():
choice = class_form.cleaned_data['class_select']
choice = int(choice)
if choice == 1:
return redirect('settings_1/')
elif choice == 2:
return redirect('/settings_2/')
elif choice == 3:
return redirect('/settings_3/')
else:
self.get(request)
else:
raise Http404
def settings_1_view(request):
context = {
}
template = "settings_1.html"
return render(request, template, context)
用户选择是通过单选按钮进行的,该值发布到views.py /检查if语句中的值,它应该将用户重定向到所选页面。 (我需要用户选择进一步计算)
POST模板choice.html:
{% extends "base.html" %}
{% load staticfiles %}
{% load static %}
{% block title %} TITLE {% endblock %}
{% block content %}
<form id="post" method="POST" enctype="application/x-www-form-urlencoded">{% csrf_token %}
<div class='container-fluid'>
<center>
<div class="col align-self-center" style="min-width:700px; margin-bottom:90px; margin-top:30px; horizontal-align: center;">
<div class="row">
<div class="col-md-4" style="background-color:#fff">
<br>
<img src="{% static 'personal/img/item1.png' %}" class="responsive-img" style='max-height:170px; width:130px' alt="item1_image">
<p>Selection 1</p>
{{ class_form.class_select.0 }}
{{ class_form.class_select.errors }}
</div>
<div class="col-md-4" style="background-color:#fff">
<br>
<img src="{% static 'personal/img/item2.png' %}" class="responsive-img" style='max-height:170px; max-width:100px' alt="item2_image">
<p>Selection 2</p>
{{ class_form.class_select.1 }}
{{ class_form.class_select.errors }}
</div>
<div class="col-md-4" style="background-color:#fff">
<br>
<img src="{% static 'personal/img/item3.png' %}" class="responsive-img" style='max-height:170px;max-width:130px' alt="item3_image">
<p>Selection 3</p>
{{ class_form.class_select.2 }}
{{ class_form.class_select.errors }}
</div>
</div>
<div class="row">
<div class="col">
<center>
<button type="submit" style="width:200">Submit</button>
</center>
</div>
</div>
</div>
</center>
</div>
</form>
{% endblock content %}
forms.朋友
class ClassChoiceForm(forms.Form):
CHOICES = [
(1, 'item1'),
(2, 'item2'),
(3, 'item3')
]
class_select = forms.CharField(label=None, widget=forms.RadioSelect(choices=CHOICES))
它确实从表单中获取用户输入,采用正确的路径,显示它但不更改浏览器中的相关URL。因此,如果我刷新页面,旧的页面将被加载。有人有想法吗?
[After Redirect same URL:][1]
[1]: https://i.stack.imgur.com/NSso3.jpg
试试这个。
return redirect('https://yourUrl.com/settings_1/')
Here is the explanation you need about rediret
Also you might want to work with the HttpResponseRedirect
Check also this answer it might help you