简体   繁体   English

form.is_valid():始终返回false

[英]form.is_valid(): always returns false

Form.is_valid() always returns false. Form.is_valid()始终返回false。 Here's my code. 这是我的代码。

#urls.py
url(r'^create/', "app.views.createpost", name="createpost"),

My models.py 我的models.py

class Post(models.Model):
    """docstring for Post"""
    post_image = AjaxImageField(upload_to='posts', max_width=200, max_height=200, crop=True, null= False, default='site_media/media/BobMarley/bob1.jpg')
    poster = models.ForeignKey(User, null= False, default=User.objects.get(username="admin")

Here's my forms.py 这是我的forms.py

#forms.py
class AjaxImageUploadForm(forms.Form):
    image = forms.URLField(widget=AjaxImageWidget(upload_to='posts'))    

view.py view.py

#views.py
def createpost(request):

    if request.method == 'POST':
        form = AjaxImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
            newpost = Post(post_image = request.FILES['image'])
            newpost.poster = request.user
            newpost.save()

            return HttpResponseRedirect('/create/')


    else:
        form = AjaxImageUploadForm() # An empty, unbound form


    posts = Post.objects.all()

    return render_to_response('create.html',{'posts': posts, 'form': form},context_instance=RequestContext(request))

The Template 模板

#create.html
{% block extra_head %}    
    {{ form.media }}
{% endblock %}
{% block body %}
    <form method="POST" enctype="multipart/form-data" action="{% url "createpost" %}">      
        {% csrf_token %} 
        {{ form.errors}}
        {{ form.as_p }}
        <button type="submit" name='image'>Upload</button>
    </form>   
{% endblock %}

The form is never valid and the error printed is "This field is required." 该表格永远无效,并且打印错误为“此字段为必填”。 And this is the form widget begin created 这是表单小部件开始创建的

<tr><th><label for="id_image">Image:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul>
<div class="ajaximage"><a class="file-link" target="_blank" href=""> <img class="file-img" src=""></a> <a class="file-remove" href="#remove">Remove</a>
<input class="file-path" type="hidden" value="" id="id_image" name="image" /> <input type="file" class="file-input" name="image"/> <input class="file-dest" type="hidden" name="image" value="/ajaximage/upload/posts/0/0/0"> <div class="progress progress-striped active"> <div class="bar"></div></div>    
</div>
</td></tr>

I am using the django package called django-ajaximage and their custom widget (AjaxImageWidget) is probably a custom text widget 我正在使用名为django-ajaximage的django包,其自定义小部件(AjaxImageWidget)可能是自定义文本小部件

class AjaxImageWidget(widgets.TextInput):

Thank you for you help 谢谢你的帮助

You do not need the init on your model class. 您不需要模型类上的init Also, null=False is the default. 同样,null = False是默认值。 I'm not sure the text 'admin' will default to the user with username admin. 我不确定文本“ admin”是否默认为用户名为admin的用户。 You would need to do: 您需要执行以下操作:

default=User.objects.get(username="admin")

You can also include the poster assignment in the creation of the object like so: 您还可以像这样在对象的创建中包括发布者分配:

newpost = Post(post_image=request.FILES['image'], poster=request.user)

To make the form validate you need to put blank=True in the field like so: 为了使表单生效,您需要像下面这样在字段中输入blank = True:

poster = models.ForeignKey(User, blank=True, default=User.objects.get(username="admin")

I would also make the form be a ModelForm: 我也将表单设为ModelForm:

class AjaxImageUploadForm(forms.ModelForm):
    class Meta:
        model=Post
        exclude=['poster',]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM