【问题标题】:How to redirect a user based on his choice in drop down?如何根据用户在下拉菜单中的选择重定向用户?
【发布时间】:2020-02-09 03:33:05
【问题描述】:

我制作了一个表单,它有一个带有两个选项的 gander Dropdown,如果用户选择 (1),我如何根据用户的选项重定向用户并提交表单会将他重定向到一个 URL,如果他的选择 (2) ) 会将他重定向到不同的 URL。 我想在视图或表单中解决它,而不是在模板中。

models.py

class User(models.Model):
    first_name = models.CharField(max_length=100)
    second_name = models.CharField(max_length=100)
    E_mail = models.EmailField(max_length=254)
    COUNTRY_CHOICES = [('saudi arabia +966','SAUDI ARABIA +966'),
                       ('oman +968','OMAN +968'),
                       ('kuwait +965','KWUAIT +965'),
                       ('Qatar +948','QATAR +948')]
    country = models.CharField(max_length=250, choices=COUNTRY_CHOICES, null=True)
    phone = models.IntegerField(null=True)
    phone_code = models.IntegerField(null=True)
    birthday = models.IntegerField(null=True)
    GANDER = [('1','Male'),
                           ('2','Female')]
    gander = models.CharField(max_length=250, choices=GANDER, null=True)

    def __str__(self):
        return self.first_name

forms.py

class UserForm(forms.ModelForm):
    class Meta:
        GANDER = (('1', 'male'), ('2', 'female'))
        model = User
        fields = ('first_name', 'second_name', 'E_mail', 'country', 'phone', 'phone_code','birthday', 'nationality',)
        widgets = {
            'first_name': forms.TextInput(attrs={'placeholder': 'الاسم الاول'}),
            'second_name': forms.TextInput(attrs={'placeholder': 'الاسم الثاني'}),
            'E_mail': forms.EmailInput(attrs={'placeholder': 'joe@schmoe.com'}),
            'nationality':  forms.Select(choices=GANDER,attrs={'class': 'form-control'}),
        }


views.py

def form_page(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            print(form)
            form.save()
            if form.nationality == 1:
                return redirect('LandingPage:thank_you')
            else:
                return redirect('LandingPage:thank_you_not')
    else:
        form = UserForm()
    posts = Article.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'LandingPage/form_page.html', {'form': form, 'posts': posts,})

我的模板

<!-- Nationalety -->
<div class="ui field">
    <label>الجنسية</label>
    {{form.nationality}}
</div>

【问题讨论】:

    标签: python django redirect django-forms django-views


    【解决方案1】:

    你做了所有困难的事情,你只需要使用form.gander(去掉 Gander 上的大写字母):

    if form.is_valid():
        form.save()
        if form.gander == 1:
            return redirect('LandingPage:thank_you_one')
        else:
            return redirect('LandingPage:thank_you_two')
    

    【讨论】:

    • 真的很简单谢谢你:),但是当我运行此代码时出现错误消息('UserForm' 对象没有属性'gander')我更新了问题并添加了 models.py。跨度>
    • 你需要把它放在widget中以便他可以使用
    • 我做了,我更新了我的代码,但仍然无法正常工作。 / 'ChoiceField' 对象的 AttributeError 没有属性 'value_from_datadict'
    猜你喜欢
    • 2014-08-05
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多