Django表单未创建对象

时间:2018-12-18 20:01:49

标签: python django forms

Django新手,非常抱歉! Form显示所有字段,但是form.is_valid()始终返回false。问题是具有完全相同的过程的表单可以工作,但不行。附有代码

view.py

def signup(request):
    form ="jugar"
    if request.method =="POST":
        form = UserForm(request.POST)
        if form.is_valid():
            userform = form.save(commit=False)
            userform.save()
            return redirect('minerva:login')
        else:
            form = UserForm()
    return render(request,'minerva/signup.html',{'form':form},)

signup.html

{%extends 'minerva/base.html'%}
{% load staticfiles %}
{% block title %}Sign Up{% endblock %}
{%block slider %}
{%endblock%}

{% block body %}
  <h2>Sign up</h2>
  <form method="POST" id="contact-form" class="post-form, contact-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default, button button-mini">SignUp!</button>
    </form>
{% endblock %}

Forms.py

from .models import Post, Comment, Message, CustomUser
from django import forms
class CommentForm(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ('author', 'content',)
class MessageForm(forms.ModelForm):

    class Meta:
        model = Message
        fields = ('sender' , 'receiver', 'msg_content',)
class UserForm(forms.ModelForm):

    class Meta:
        model = CustomUser
        fields = ('username' , 'email' , 'password' , 'ProfileImage' , 'MasterOf', 'intro',)

Models.py

from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils import timezone
import datetime

class CustomUser(models.Model):
    username = models.CharField(max_length=250)
    email = models.EmailField(max_length=250)
    password = models.CharField(max_length=250)
    ProfileImage = models.ImageField()
    MasterOf = models.ForeignKey(Subject, on_delete=models.SET_NULL, null=True)
    intro = models.TextField()
    def __str__(self):
        return self.username

0 个答案:

没有答案