Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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/21.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 如何将表单传递给基于类的通用视图(TodayaArchiveView)?_Python_Django_Python 2.7_Django Generic Views_Django 1.4 - Fatal编程技术网

Python 如何将表单传递给基于类的通用视图(TodayaArchiveView)?

Python 如何将表单传递给基于类的通用视图(TodayaArchiveView)?,python,django,python-2.7,django-generic-views,django-1.4,Python,Django,Python 2.7,Django Generic Views,Django 1.4,url.py views.py from django.conf.urls import patterns, include, url import os.path from crm.views import * urlpatterns += patterns('', (r'^test/$', tView.as_view()), ) tView是通用视图。。。。 WorkDailyRecord正在models.py中定义模型 我想将表单('WorkDailyRecordForm'fo

url.py

views.py

from django.conf.urls import patterns, include, url
import os.path
from crm.views import *

urlpatterns += patterns('',
    (r'^test/$', tView.as_view()),
)
tView是通用视图。。。。 WorkDailyRecord正在models.py中定义模型

我想将表单('WorkDailyRecordForm'forms.py中的类)传递给模板(仅限于workdailyrecord.html)

怎么做

forms.py

from django.views.generic import TodayArchiveView
from crm.forms import *
from crm.models import *

class tView(TodayArchiveView):
    model = WorkDailyRecord
    context_object_name = 'workDailyRecord'
    date_field = 'date'
    month_format = '%m'
    template_name = "onlyWorkDailyRecord.html"
    form_class = WorkDailyRecordForm ################## ADD
仅限WorkDailyRecord.html

from django import forms

class WorkDailyRecordForm(forms.Form):
    contents = forms.CharField(label='',
            widget=forms.Textarea(attrs={'placeholder': 'contents', 'style':'width:764px; height:35px;'}),
            required=False,
        )
    target_user = forms.CharField(label='',
            widget=forms.TextInput(attrs={'placeholder': 'target', 'style':'width:724px'}),
            required=False,
        )

{{form.as_table}}
작성

在基于类的视图中,您需要提到
表单\u类

<form method="post" action="." class="form-inline" id="save-form">
    {{ form.as_table }}
    <button type="submit" class="btn btn-mini" id="workDailyRecord-add">작성</button>
</form>
一个简单的例子:

form_class = WorkDailyRecordForm

要了解更多信息,请阅读

我在将表单类传递到泛型时遇到了问题,我可以通过重写get_context_data()函数来实现,以防有人也在寻找它

mozilla文档中的一个示例:

class tView(TodayArchiveView):
    form_class = WorkDailyRecordForm
    template_name = 'onlyWorkDailyRecord.html'
    model = WorkDailyRecord

    def form_valid(self, form, **kwargs):
        instance = form.save(commit=True)
        return HttpResponse('Done')

    def dispatch(self, request, *args, **kwargs):
        return super(tView, self).dispatch(request, *args, **kwargs)
将表单对象传递到上下文字典中的键中


可以找到更多信息。

您是否为类视图编写了
分派
函数?Aamir Adnan//可能。。。不,我不知道该怎么办?我添加了一个简单的例子。希望这会有帮助。谢谢!顺便说一句,我在“onlyWorkDailyRecord.html”中写“{form.as_table}}”。这是对的吗?是的,这是对的<代码>表单.as\u p或
表单.as\u表
两者都应起作用。或者手动迭代表单字段。
class BookListView(generic.ListView):
    model = Book

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get the context
        context = super(BookListView, self).get_context_data(**kwargs)
        # Create any data and add it to the context
        context['some_data'] = 'This is just some data'
        return context