Django表单textarea没有出现在模板中 [英] Django form textarea doesn't appear in template

查看:67
本文介绍了Django表单textarea没有出现在模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,正在尝试创建一个表单。关键是,当我呈现表单时,不会显示文本字段,但会显示按钮。

I'm new in Django and I'm trying to create a form. The thing is that when I render the form, the textfield doesn't appear, but the button does.

该项目是类似Twitter的应用程序,表单位于用户将文本放进去然后发布。

The project is a twitter-like app, the form is where the user puts the text to then post it.

forms.py:

from django import forms

class TweetForm(forms.Form):
     text = forms.CharField(widget=forms.Textarea(attrs={'rows': 1, 'cols': 85}), max_length=160)
     country = forms.CharField(widget=forms.HiddenInput())

view.py:

class PostTweet(View):
    def post(self, request, username):
        form = TweetForm(self.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()
            words = form.cleaned_data['text'].split(" ")
            for word in words:
                if word[0] == "#": #Separamos todas las letras del tweet y si empieza con #, se va a crear un hashtag de esa palabra
                    hashtag, created = HashTag.objects.get_or_create(name=word[1:])
                    hashtag.tweet.add(tweet)
        return HttpResponse('/user/' +username)

model.py:

class HashTag(models.Model):
    #HashTah Model:
    name = models.CharField(max_length=64, unique=True)
    tweet = models.ManyToManyField(Tweet)

    def __unicode__(self):
        return self.name

template:

{% extends "base.html" %}


{% block content %}

<div class="row clearfix">
    <div class="col-md-12 column">
       <form method="post" action="post/">{% csrf_token %}
            <div class="col-md-8 col-md-offset-2 fieldWrapper">
               {{ form.text.errors }}
               {{ form.text }}
            </div>
           {{ form.country.as_hidden }}
           <div>
               <input type="submit" value="Post">
           </div>
        </form>
    </div>
    <h3>&nbsp;</h3>
    <div>
        {% for tweet in tweets %}
        <div class="well">
            <span>{{ tweet.text }}</span>
        </div>
        {% endfor %}
    </div>
</div>
{% endblock %}

模板图片:
在此处输入图片描述

推荐答案


  1. 问题是您没有将表单传递到模板中。您可以通过继承视图中的 get_context_data 方法来做到这一点。

使用 FormView 而不是简单的 View ,定义 form_class template_name ,它将起作用。代替 post 方法,使用 form_valid form_invalid 方法如果需要,处理无效和无效的响应。由于您使用的是 ModelForm ,因此您可以省略所有方法并尽可能简化此视图,只需添加 success_url 属性

Use FormView instead of simple View, define form_class and template_name, and it will work. Instead of post method, use form_valid and form_invalid methods to handle vaild and invalid response, if you need. Since you are using ModelForm, you can omit all methods and keep this view as simple as possible, just adding success_url property to it.

请阅读有关基于类的视图的信息,并深入研究FormView。会有所帮助的:)

Please read information about ClassBased views, and dig into FormView. It will help :)

这篇关于Django表单textarea没有出现在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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