Django表单发表什么都不做 [英] Django form posting does nothing

查看:241
本文介绍了Django表单发表什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含以下字段的图像发布表单:标题,说明,图像文件以及公共或不是布尔值。我的问题是它什么都不做。似乎发布,我可以看到它在开发服务器控制台,但没有,没有图像在媒体/照片,数据库中没有任何...奇怪的是,当我从django管理页面创建一个Image对象时,它工作得很好所以它必须在视图或模板中,但我可能是错误的。以下是代码:

I'm making an Image posting form with thes fields: Title, Description, Image file, and a public or not boolean. The issue I have is that it does nothing. It seems to post as I can see it in the dev server console, but then nothing, no image in media/photos, nothing in the database either... Weird thing is that when I create an Image object from the django admin page, It works perfectly fine. So it must be in the view or template, but I may be wrong. Here's the code:

<!--The template-->
{% if user.is_authenticated %}

    <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 %}

查看:

#The view
def Publish(request):
    reg = False
    sauvegarde = False
    if request.user.is_authenticated():
        if request.method == "POST":
            form = ImagePublish(request.POST, request.FILES)
            if form.is_valid():
                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(request.POST, request.FILES)
    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())

表单:

#The form
class ImagePublish(forms.Form):
    titre = forms.CharField(max_length=100)
    description = forms.CharField(widget=forms.Textarea)

    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
    public = forms.BooleanField(required=False)

就是这样,我可以添加所需代码的任何其他部分,一切都是导入的,我也想指出我没有错误的事实,我设置为酷,图像上传的消息不显示,如果这可以显示任何人的方向...

That's it, I can add any other part of the code you need and everything is imported, also I would like to point the fact that I got no error, and the message I set to say like "cool, Image uploaded" doesn't show up, If that can show anyone the good direction...

推荐答案

我刚刚解决了我的问题,实际上是没有填充连接用户的auteur字段,所以它没有验证原因是空的,图像没有发布。我刚才在我的视图中添加了pic.auteur = request.user,并且这样做。如果你需要它只是问,我会发布新的代码!再见

I just solved my issue, actually it was the auteur field that didn't fill itself with connected user, so it didn't validate cause it was empty and the Image wasn't posting. I just added "pic.auteur = request.user" in my view and it did it. If you need it just ask and I'll post the new code! bye

这篇关于Django表单发表什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆