小编sun*_*ysm的帖子

Django modelform如何添加确认密码字段?

在这里,我需要confirmation password在我的表单中添加一个额外的.我使用了Django的模型.我还需要验证两个密码.如果,它必须引发验证错误password1 != password2.

这是我的forms.py:

class UserForm(forms.ModelForm):
    password=forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model=User
        fields=('username','email','password')

class UserProfileForm(forms.ModelForm):
    YESNO_CHOICES = (('male', 'male'), ('female', 'female'))
    sex = forms.TypedChoiceField(choices=YESNO_CHOICES, widget=forms.RadioSelect)
    FAVORITE_COLORS_CHOICES=(('red','red'),('blue','blue'))
    favorite_colors = forms.MultipleChoiceField(required=False,widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)
    dob = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), 
                                 input_formats=('%d/%m/%Y',))

    class Meta:

        model=UserProfile
        fields=('phone','picture','sex','favorite_colors','dob')
Run Code Online (Sandbox Code Playgroud)

这是我的注册功能:

def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)



        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'picture' in request.FILES:
                profile.picture = …
Run Code Online (Sandbox Code Playgroud)

django-forms password-confirmation

16
推荐指数
2
解决办法
1万
查看次数

如果条件在 html 中

我是新来的django。在这里,我尝试使用django. 我想显示'你输入正确'

if selected_choice='Yellow'
Run Code Online (Sandbox Code Playgroud)

这是我的代码

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
    # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
        'question': question,
        'error_message': "You didn't select a choice.",
    })
    else:

        selected_choice.votes += 1
        selected_choice.save()
        context_dict={}
        context_dict['selected_choice']=selected_choice
        context_dict['question']=question

        return render(request, 'polls/result.html', context_dict)
Run Code Online (Sandbox Code Playgroud)

html文件

<h1>{{ question.question_text }}</h1>
{% if selected_choice  %}
    {% if 'Yellow' %}

    <p> you entered correct </p>
    {% endif %}

{% endif %}


<ul> …
Run Code Online (Sandbox Code Playgroud)

html css python if-statement

1
推荐指数
1
解决办法
3万
查看次数