0

I have models.py and forms.py, views.py and as bellow . I want only alpha numeric inputs . after submitting the form, i am getting the error :'ErrorDict' object has no attribute 'status_code'

Kindly suggest .

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
  • The problem is in your view, which you have not shown. Commented May 14, 2017 at 16:30
  • @DanielRoseman , sir I have submitted my views.py and other details , kindly suggest . Commented May 14, 2017 at 16:50

2 Answers 2

2

Edit your view,

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})
Sign up to request clarification or add additional context in comments.

6 Comments

Then, could you do me a favour and tag the answer as selected?
My bad.. I didn't see the above answer
Daniel Roseman answered the same 5 mints before you , and that worked . So i have marked his ans as selected . In my next question i will do it for you . (My next question will be how to take regional language as input in forms in django , i can take regional language as input in forms , but unable to edit with admin interface . its giving some error . )
Thank you very much
i have already done it for u , my reputation is less than 15 , so i think it will not count for u .
|
0

When the form is not valid, your else statement returns form.errors directly. That is not a valid thing to return from a view; views always need to return an HTTP response.

You should remove that first else statement, and let execution fall through to the final line so that your template is rendered with the invalid form. You should also modify the template so that it actually outputs the contents of form.errors.

1 Comment

Sir , Thank you . Now its working fine . If you have any reference material for django , kindly suggest .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.