Reputation: 4862
url.py
from django.conf.urls import patterns, include, url
import os.path
from crm.views import *
urlpatterns += patterns('',
(r'^test/$', tView.as_view()),
)
views.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
tView is generic view.... WorkDailyRecord is defining model in models.py
I want to pass form('WorkDailyRecordForm' class in forms.py) to template(onlyWorkDailyRecord.html)
How??
add
forms.py
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,
)
onlyWorkDailyRecord.html
<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>
Upvotes: 0
Views: 1166
Reputation: 137
I had trouble with passing form class into a generic, I was able to do it by overriding the get_context_data() function, in case anyone was looking for this as well.
An example from the mozilla documentation:
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
You would pass the form object into a key in the context dictionary.
Can find more information here.
Upvotes: 0
Reputation: 39659
In the class based views you need to mention the form_class
:
form_class = WorkDailyRecordForm
A simple example:
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)
For more information read generic-editing in class based views
Upvotes: 0