将表单数据呈现到同一页面

时间:2016-11-27 13:22:51

标签: django django-forms django-templates

表单有效。输入显示在规则上。但模板显示空白页面。救命啊!

views.py

class StandingsView(FormView):
    form_class = SelectTeam
    template_name = 'teamsports/standings.html'
    model = Teams
    success_url = None

    def form_valid(self, form):
        form=SelectTeam
        data = self.request.GET.get('team_name')
        team = Teams.objects.filter(team_name__icontains=data).values()
        return render(self.request, 'teamsports/standings.html', {'team': team})

template.html

{% extends 'teamsports/base.html' %}

    {% block content %}
    <h1>Pick your team, chump</h1>

<form form_name = SelectTeam action="" method="GET">
    {{ form }}
    <input type="submit" value="Submit" />
    {{ form.errors }}
</form>    

<h1> {{ team }} </h1>
    {% for team in Teams %}
        <h1>{{ team.team_name }}</h1>
    {% endfor %}
{% endblock %}

来自settings.py的更新 - 虽然我没有传递任何变量,但我的基础和主页模板已扩展。

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        '/home/mrsaltz/thepub/teamsports/templates/teamsports',
        ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
                    ],
    },
},
]

1 个答案:

答案 0 :(得分:0)

我只是尝试在我的环境中迭代你的情况,一切正常。我的意思是我的模板中有表单和模型。对模板命名可能有问题。

您的视图中已有字典{'team:team'}。所以你的观点应该是这样的:

url(r'^standings/$', views.StandingsView.as_view()),

您的模板主题应如下所示:

TEMPLATES = [
    {                                                                                                   
        'BACKEND': 'django.template.backends.django.DjangoTemplates',                                   
        'DIRS': [
                        os.path.join(BASE_DIR, 'templates'),                                            
                ],
        'APP_DIRS': True,
        'OPTIONS': {                                                                                    
            'context_processors': [                                                                     
                'django.template.context_processors.debug',                                             
                'django.template.context_processors.request',                                           
                'django.contrib.auth.context_processors.auth',                                          
                'django.contrib.messages.context_processors.messages',                                  
            ],                                                                                          
        },
    },          
]    

然后你的项目的根目录必须有template文件夹。 在template文件夹中,您必须拥有base.html文件,如下所示:

<body>
   {% block main %}{% endblock %}
</body>

然后在模板文件夹中创建文件夹,在您的案例中调用与您的应用相同的文件夹,因为我将其teamsports覆盖,那么您的standings.html文件应如下所示:

{% extends 'base.html' %}
{% block main %}
    <h1>Pick your team, chump</h1>

<form form_name = SelectTeam action="" method="GET">
    {{ form }}
    <input type="submit" value="Submit" />
    {{ form.errors }}
</form>    

<h1> {{ team }} </h1>
    {% for team in Teams %}
        <h1>{{ team.team_name }}</h1>
    {% endfor %}
{% endblock %} 

请注意,如果{% block main %}{% endblock %} base.html standings.html中有Failed to create Specman's temporary directory `/local/temp/specman' sn_complile.sh efile.e -t temp/

相关问题