POST后,Django表单无法呈现

时间:2017-08-26 10:16:25

标签: django django-forms

在下面的示例中,我试图将所有错误消息显示在模板中。我已经尝试了所有论坛中提到的几乎所有内容。以下是我的代码。请告诉我我缺少的东西

表格

class RegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model   = User
        fields  = (
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2'
        )

        def save(self, commit=True):
            user = super(RegistrationForm, self).save(commit=False)
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.email = self.cleaned_data['email']

            if commit:
                user.save()

            return user

查看

def register (request):
    print(request.method)
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            print("Saved")
            return render(request, 'main/product.html',{})
        else:
            print("Some Error", form.errors)
            args = {'form':form,'carousel':'display:none'}      
            print(args['form'])
            return render(request, 'accounts/register.html',args)
        # if a GET (or any other method) we'll create a blank form
    else:
        form = RegistrationForm()

    args = {'form':form,'carousel':'display:none'}
    return render(request, 'accounts/register.html',args)

HTML模板

<form id="register" method="post" action="{% url 'register'  %}">
    {% csrf_token %}
    <div class="form-register-with-email">  
    <div class="form-white-background"> 
   <div class="form-title-row">
       <h1>Create an account</h1>
   </div>
    {{ form.errors }}
    {% for field in form %} 
    <div class="form-row">  
        <label>
        <span>{{field.label}}</span> 
        {{ field }}
        </label>
    </div>  
    {% endfor %}

    <div class="form-row">
      <button type="submit">Register</button>
   </div>   
    </div>
    </div>  
</form>

在我按下提交后触发视图,当我在render语句之前打印它时,表单显示所有错误消息。但是提交后,渲染实际上并不会触发屏幕上的任何更改。

下面是打印的django输出。

<ul class="errorlist"><li>username<ul class="errorlist"><li>A user with that username already exists.</li></ul></li><li>password2<ul class="errorlist"><li>The password is too similar to the username.</li><li>This password is too short. It must contain at least 8 characters.</li><li>This password is too common.</li></ul></li></ul>
<tr><th><label for="id_username">Username:</label></th><td><ul class="errorlist"><li>A user with that username already exists.</li></ul><input autofocus="" id="id_username" maxlength="150" name="username" type="text" value="test" required /><br /><span class="helptext">Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.</span></td></tr>
<tr><th><label for="id_first_name">First name:</label></th><td><input id="id_first_name" maxlength="30" name="first_name" type="text" value="test" /></td></tr>
<tr><th><label for="id_last_name">Last name:</label></th><td><input id="id_last_name" maxlength="30" name="last_name" type="text" value="test" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input id="id_email" name="email" type="email" value="test@gmail.com" required /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input id="id_password1" name="password1" type="password" required /></td></tr>
<tr><th><label for="id_password2">Password confirmation:</label></th><td><ul class="errorlist"><li>The password is too similar to the username.</li><li>This password is too short. It must contain at least 8 characters.</li><li>This password is too common.</li></ul><input id="id_password2" name="password2" type="password" required /><br /><span class="helptext">Enter the same password as before, for verification.</span></td></tr>
[26/Aug/2017 10:01:19] "POST /register/ HTTP/1.1" 200 5732

我错过了什么?

2 个答案:

答案 0 :(得分:0)

这样做

print(request.method)
if request.method == 'POST':
    form = RegistrationForm(request.POST)
    if form.is_valid():
       form.save()
       print("Saved")
       return render(request, 'main/product.html',{})
else:
   form = RegistrationForm()
   args = {'form':form,'carousel':'display:none'}
   return render(request, 'accounts/register.html',args)

答案 1 :(得分:0)

解决了这个问题。这是我的javascript文件中的一个旧代码导致了这个问题。一旦修复,表格就会正确提交。