输入文本字段不显示在浏览器上 - Django

时间:2016-09-20 12:06:31

标签: django django-forms django-templates

我按照一本书来学习Django,但现在我被卡住了,因为我无法渲染我应该的文本字段。我正在使用Django 1.9版,通常在Firefox上渲染。

我的django 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())

我有两个html文件:base.html和profile.html

<!DOCTYPE html>
    {% load static from staticfiles %}
<html>


<head>
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="{% static 'js/bootstrap.min.js' %}" rel="stylesheet">

</head>

<body>
    <nav class="navbar navbar-fixed-static navbar-default" role="navigation">
        <a class="navbar-brand" href="#">MyTweets</a>
        <p class="navbar-text navbar-right container-fluid">User Profile Page</p>
    </nav>

    <div class="container">
        {% block content %}
        {% endblock %}
    </div>

    <nav class="navbar navbar-default navbar-fixed-bottom" role="navigation">
        <p class="navbar-text navbar-right">Footer </p>
    </nav>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>
</body>
</html>



<!DOCTYPE html>
<!--esse extends significa que ele vai entrar no bloco definido no arquivo base.html-->
<!-- primeiro esse profile era apenas pra mostrar os twitters, mas depopis adicionamos o campo de

adicionar um novo twitter, utilizando form, que foi feito no aqquivo <forms class="py"></forms>-->
{% extends "tweets/base.html" %}

<!--definicao dos blocos, ou seja, tera o mesmo header, body ..., o que muda eh o content-->    
{% block content %}
<div class="container" class="row clearfix">
    <div class="col-md-12 column">
    <!--Form para um novo twitter-->
        <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>

    <!--loop que mostra os tweets-->
    <div class="col-md-12 column">
        <!-- ver a logica disso no views-->
        {% for tweet in tweets %}
        <!-- a classe well ou wellbox, da o efeito de insercao-->
        <div class="well">
            <span>{{tweet.text}}</span>
        </div>
        {% endfor %}
    </div>
</div>
{% endblock %}

因此,渲染的结果如下图所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,所以Django不允许你在forms.Form

的情况下为表单字段添加 attrs

您需要使用ModelForm。像这样:

from django import forms

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        widgets = {
          'text': forms.Textarea(attrs={'rows':1, 'cols':85}),
        }

但是对于您的用例,我建议您在模板中使用https://pypi.python.org/pypi/django-widget-tweaks这样的包:

首先是pip install django-widget-tweaks

现在在 settings.py installed_apps 中添加 django-widget-tweaks ,如下所示:

INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'widget_tweaks',
)

现在使用它们离开你的模板:

{% load widget_tweaks %}
...
{{ form.text|attr:"rows:1"|attr:"cols:85" }}

看到这个 - &gt; How can i set the size of rows , columns in textField in Django Models