验证表单时,'ErrorDict'对象没有属性'status_code'

时间:2017-05-14 16:25:56

标签: python django forms

我有models.py和forms.py,views.py以及如下所示。我只想要字母数字输入。提交表单后,我收到错误:'ErrorDict'对象没有属性'status_code'

请建议。

from django.core.validators import RegexValidator
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed.')



class News_Post(models.Model):
    Country=models.CharField(max_length=20, validators=[alphanumeric])
    State=models.CharField(max_length=20, validators=[alphanumeric])
    District=models.CharField(max_length=20, validators=[alphanumeric])
    Area=models.CharField(max_length=20, validators=[alphanumeric])
    Photo_link=models.CharField(max_length=50,blank=True)
    News_Title=models.CharField(max_length=200, validators=[alphanumeric])
    News=models.TextField(validators=[alphanumeric])
    created_date=models.DateTimeField(auto_now_add=True,)
    author = models.CharField(max_length=20)

    def __str__(self):
            return self.News_Title


forms.py:

from django import forms
from django.forms import ModelForm


class NewsForm(forms.ModelForm):
    Country=forms.CharField(max_length=20, required=False, help_text='Optional.')
    State=forms.CharField(max_length=20, required=False, help_text='Optional.')
    District=forms.CharField(max_length=20, required=False, help_text='Optional.')
    Area=forms.CharField(max_length=20, required=False, help_text='Optional.')
    Photo_link=forms.CharField(max_length=50, required=False, help_text='Optional.')
    News_Title=forms.CharField(max_length=200, required=True, help_text='Required')
    News=forms.CharField(widget=forms.Textarea)

    class Meta:
            model = News_Post
            fields =   ('Country','State','District','Area','Photo_link','News_Title', 'News', )
            exclude = ["author"]

Views.py:

.                                                                                
  def new_submit(request):
        if request.method == 'POST':
            form = NewsForm(request.POST)
            if form.is_valid():
                    c=form.save(commit=False)
                    c.author = request.user
                    c.save()
                    return redirect(my_submitted_news )
            else:
                    return form.errors
    else:
            form = NewsForm()
    return render(request,'new_submit.html', {'form': form})

new_submit.html:

 {% block content %}
{% if form.errors %}
<p style="color: red"> Please try again.</p>
 {% endif %}
 <form method="post">
 {% csrf_token %}
 <input type="hidden" name="next" value="{{ next }}" />
 {% for field in form %}
  <p>
    {{ field.label_tag }}<br>
    {{ field }}
    {% if field.help_text %}
    <small style="color: grey">{{ field.help_text }}</small>
    {% endif %}
    {% for error in field.errors %}
      <p style="color: red">{{ error }}</p>
    {% endfor %}
   </p>
  {% endfor %}
<button type="submit">Submit News</button>

{%endblock%}

2 个答案:

答案 0 :(得分:1)

修改您的观点,

def new_submit(request):
    if request.method == 'POST': 
        form = NewsForm(request.POST) 
        if form.is_valid():
            c=form.save(commit=False)
            c.author = request.user
            c.save()
            return redirect('your_url_name' )
        else:
            return render(request, 'template_name', dict(form, form))
    else:
        form = NewsForm()
        return render(request,'new_submit.html', {'form': form})

答案 1 :(得分:0)

当表单无效时,else语句直接返回form.errors。从视图中返回并不是一件有效的事情;视图总是需要返回HTTP响应。

您应该删除第一个else语句,然后让执行进入最后一行,以便使用无效表单呈现模板。您还应该修改模板,使其实际输出form.errors

的内容