Domanda

Okay so I have django model and created a form of that model. Here is my template:

<form method="post" action="">{% csrf_token %}
    {{ form.first_name }} {{form.last_name }} <br>
    {{ form.username }} {{ form.password }} <br>
    {{ form.date_of_birth_month }} {{ form.date_of_birth_day }} {{ form.date_of_birth_year }}
    <input type="submit" value="Register"/>
</form>


{% for field, error in form.errors.items %}
    {% if forloop.counter == 1 %}
        {{ error | striptags }}
    {% endif %}
{% endfor %}

Now, here is my model of that form.

class Users(models.Model): 
    months = (
        ('Month','Month'), ('January', 'January'), ('February','February'), ('March','March'), ('April','April'), ('May','May'), ('June','June'), ('July','July'), ('August','August'), ('September','September'), ('October','October'), ('November','November'), ('December','December'),
)
    days = (
        ('Day', 'Day'), ('1','1'), ('2','2'), ('3','3'), ('4','4'), ('5','5'),)
    years = (
        ('Year','Year'), ('2013','2013'), ('2012','2012'), ('2011','2011'), ('2010','2010'), ('2009','2009'), ('2008','2008'),)

    user_id = models.AutoField(unique=True, primary_key=True)
    username = models.SlugField(max_length=50, unique=True)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    password = models.SlugField(max_length=50)

    date_of_birth_month = models.CharField(verbose_name='', max_length=9, choices=months, default='Month')
    date_of_birth_day = models.CharField(verbose_name='', max_length=3, choices=days, default='Day')
    date_of_birth_year = models.CharField(verbose_name='', max_length=4, choices=years, default='Year')

Now, the default for the month choices box is 'Month' and the user name click the drop down and pick which month he was born in. Same for Day and Year. However, this makes Month, Day and Year an option which the user can pick. How can I make it so that if the user selects 'Month' as the month, 'Day' as the day or 'Year' as the year, then raise an error message saying 'Please select a valid Month / Day / Year'?

my view is here:

def home_page(request):
    form = UsersForm()
    if request.method == "POST":
        form = UsersForm(request.POST)

        if form.is_valid():
            form.save()
    c = {}
    c.update(csrf(request))
    c.update({'form':form})
    return render_to_response('home_page.html', c)
È stato utile?

Soluzione

Write the clean method for fields in your form:

class UsersForm(form.Form):
    # form fields here

    def clean_month(self):
        month = self.cleaned_data.get('month')
        if month:
            try:
                month = int(month)
            except:
                raise forms.ValidationError('Invalid month')
            if month < 1 or month > 12:
                raise forms.ValidationError('Invalid month')

        return month

Do this for rest of fields which you want to validate. More help is in Documentation

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top