Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么表格是无效的?_Python_Django_Twitter - Fatal编程技术网

Python 为什么表格是无效的?

Python 为什么表格是无效的?,python,django,twitter,Python,Django,Twitter,我最近在《学习Django Web开发》一书中学习了Django。我想使用make thepost tweet功能,但我总是会得到下面的错误 view tweets.views.PostTweet未返回HttpResponse对象。 它没有返回任何结果 经过一系列测试,我发现表单无效 这是视图.py class PostTweet(View): """ Tweet post form availbale on page/user/<username> URL

我最近在《学习Django Web开发》一书中学习了Django。我想使用make thepost tweet功能,但我总是会得到下面的错误

view tweets.views.PostTweet未返回HttpResponse对象。 它没有返回任何结果

经过一系列测试,我发现表单无效

这是视图.py

class PostTweet(View):
    """
    Tweet post form availbale on page/user/<username> URL
    """
    form_class = TweetForm
    template_name = 'profile.html'

    def post(self, request, username):
        form = self.form_class(request.POST)
        if form.is_valid():
            user = User.objects.get(username=username)
            tweet = Tweet(text=form.cleaned_data['text'],
                          user=user,
                          country=form.cleaned_data['country'])
            tweet.save()
            form.save()
            return HttpResponseRedirect('/user/'+username)

任何人只要给我一个提示都将不胜感激。:-)

当表单有效时,视图将执行操作,但当表单无效时,执行重定向的块将不运行,并且该方法将不执行任何操作而退出,默认情况下不返回任何操作

form.is\u valid()
返回false时,您需要添加一些操作来处理这种情况,例如错误消息或错误页面重定向

{% extends "base.html" %}
{% block content %}
<div class="row clearfix">
  <div class="col-md-6 col-md-offset-3 column">
    <form id="search-form" method="POST" action="post/">{% csrf_token %}
      <div class="col-md-8 fieldWrapper">
        <textarea cols="85" id="id_text" maxlength="160" name="text" rows="1"></textarea>
        <input id="id_country" name="country" type="hidden" />
        <span class="input-group-btn">
          <button class="btn btn-default" type="submit">Post</button>
        </span>
      </div>
    </form>
  </div>
  <h3>&nbsp;</h3>
  <div class="col-md-12 column">
    {% for tweet in tweets %}
      <div class="well">
        <span>{{ tweet.text}}</span>
      </div>
    {% endfor %}
  </div>
</div>
{% endblock %}
from django import forms

class  TweetForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea.is_required,max_length=160)
    country = forms.CharField(widget=forms.HiddenInput())