为什么Django views.py重定向到另一个视图而不更改URL?

时间:2019-04-04 20:58:30

标签: django python-3.x url-redirection

我试图在用户输入后从我的views.py重定向到另一个URL-视图实际上发生了变化,但是不幸的是,浏览器的URL保持不变。

urls.py:


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.py


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.py

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
相关问题