Django模型形式{is_valid()}

时间:2016-12-03 12:32:14

标签: python django forms

我有我的表单并且我检测到程序没有进入is_valid()因为在提交之后它将我重定向到welcomeUser.html,它位于if:is_valid()的else中。我不知道还有什么可以尝试。提前致谢

models.py

from django.db import models


class User(models.Model):

    user_name = models.CharField(max_length=10)
    user_alias = models.CharField(max_length=10)
    user_password = models.CharField(max_length=10)
    user_community = models.CharField(max_length=10)
    user_email = models.EmailField(max_length=10)
    user_points = models.IntegerField(blank=True, null=True)
    user_ratio = models.FloatField(blank=True, null=True)

forms.py

from django.forms import ModelForm
from models import User


class UserForm(ModelForm):

    class Meta:

        model = User
        exclude = ['user_points', 'user_ratio']

views.py

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.template import RequestContext

from models import User
from forms import UserForm


def signup(request):

    if request.method == 'POST':
        form_to_sign_up = UserForm(request.POST)

        if form_to_sign_up.is_valid():
            new_user = form_to_sign_up.save()
            new_user.save()
            return HttpResponseRedirect('signupsuccess.html')

        else:
            return render_to_response('welcomeUser.html')

    else:

        form_to_sign_up = UserForm()
        return render_to_response('sign_up.html', {'form': form_to_sign_up}, context_instance=RequestContext(request))


def signupsuccess(request):

        users = User.objects.all()
        return render_to_response('signupsuccess.html', {'users': users})

def home_user(request):

    return render(request, 'home_user.html')

sign_up.html

<form method="post">{% csrf_token %}

                <div class="new_user_input">
                    <label for="user_name">Nombre: </label> {{form.user_name}}            

                </div>

                <div class="new_user_input">
                    <label for="user_alias">Alias: </label>
                    {{form.user_alias}}
                </div>

                <div class="new_user_input">
                    <label for="user_password">Contraseña: </label>
                    {{form.user_name}}
                </div>

                <div class="new_user_input">
                    <label for="user_email">Email: </label>
                    {{form.user_email}}
                </div>

                <div class="new_user_input" id="save_save_button_sign_up_block">
                    <button type="submit" class="save_button_sign_up">¡Registrar!</button>
                </div>

                {% csrf_token %}
            </form>

0 个答案:

没有答案