有没有办法从django' form.is_valid中排除BooleanField?

时间:2014-04-17 14:15:39

标签: django forms model

好吧,在我解决这个问题的路上:Django form posting does nothing事实证明问题可能来自复选框输入,它不会验证form.is_valid条件。这就是我想跳过这一步的原因。用{{form}}替换手写模板表单不起作用,并且不给booleanfield一个blank = true参数。在此先感谢,随时提出任何可能有用的建议。再见

-----------------------------------------------的模板

{% if user.is_authenticated %}
    <!--The template-->
    <h1>Ajouter une image</h1>
    <h2>Deconnexion:</h2><a href="/Gallery/Disconnect/">Ici</a>
    <p>
    <form method="post" enctype="multipart/form-data" action=".">
    {% csrf_token %}
    <p><label for="id_titre">Titre:</label>
    <input id="id_titre" type="text" name="Titre" maxlength="100" /></p>
    <p><label for="id_description">Description:</label>
    <textarea name="Description" id="id_description" ></textarea></p>
    <p><label for="id_i">Image:</label>
    <input type="file" name="Image" id="id_i" /></p>
    <p><label for="id_public"><input type="checkbox" name="public" id="id_public"/>Public <!-- Attention, Internet est un lieu public, meme si cette case n'est pas cochée, votre image devient publique une fois postée. elle ne sera cependant pas affichée dans la gallerie aleatoire ou dernieres images. -->
    </label></p>
    <input type="submit"/>
    </form>
    </p>
    {% if sauvegarde %}
    <p>Image saved.</p>
    {% endif %}
{% else %}
    <h1>Connect</h1>
        {% if error %}
        <p><strong>wrong password, or user does not exist, sorry</strong></p>
        {% endif %}
    <form method="post" action=".">
    {% csrf_token %}
    <p><label for="id_username">Username:</label>
    <input id="id_username" type="text" name="username" maxlength="30" /></p>
    <p><label for="id_password">Password:</label>
    <input id="id_password" type="password" name="password" maxlength="30" /></p>
    <input type="submit"/>
    </form><h1>Or Register <a href="/Gallery/Register/">here</a></h1>
{% endif %}

----------------------------------------------- 查看

def Publish(request):
reg = False
sauvegarde = False
print("you did it at first")
if request.user.is_authenticated():
    print("you did it then")
    if request.method == "POST":
        print("are you kidding me?")
        form = ImagePublish(request.POST, request.FILES)
        if form.is_valid():
            print("I'll drink one more Burn if that message doesn't show up")
            form.save()
            print("you did it") 
            #pic = Image()
            #pic.titre = form.cleaned_data["titre"]
            #pic.description = form.cleaned_data["description"]
            #pic.i = form.cleaned_data["i"]
            #pic.public = form.cleaned_data["public"]
            #pic.save()

            sauvegarde = True
        else:
            form = ImagePublish()
else :
    if request.method == "POST":
        form = ConnexionForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]  # Nous récupérons le nom d'utilisateur
            password = form.cleaned_data["password"]  # … et le mot de passe
            user = authenticate(username=username, password=password)  #Nous vérifions si les données sont correctes
            if user:  # Si l'objet renvoyé n'est pas None
                login(request, user)  # nous connectons l'utilisateur
            else: #sinon une erreur sera affichée
                    error = True
    else:
        form = ConnexionForm()

return render(request, 'Publier.html',locals())

-----------------------------------------------的表格

class ImagePublish(forms.ModelForm):
class Meta:
    model = Image
def clean_content(self):
    if content != None:
        content = self.cleaned_data['content']
        content_type = content.content_type.split('/')[0]
        if content_type in settings.CONTENT_TYPES:
            if content._size > int(settings.MAX_UPLOAD_SIZE):
                raise forms.ValidationError(_(u'Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(content._size)))
        else:
            raise forms.ValidationError(_(u'File type is not supported'))
    return content

-----------------------------------------------的模型

class Image(models.Model):
titre = models.CharField(max_length=100)
description = models.TextField(null=True)
date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de parution")
i = models.ImageField(upload_to="photos/")
public = models.BooleanField(default=True)
auteur = models.ForeignKey('auth.User') 



def __unicode__(self):

    return u"%s" % self.titre

0 个答案:

没有答案