我有以下表格,我想用Django crispy表格渲染它。
这是我的views.py
class RehabilitationSessionCreate(CreateView):
model = RehabilitationSession
form_class = RehabilitationSessionForm()
success_url = reverse_lazy('rehabilitationsessions:list')
fields = ['patient','medical','therapist','status','date_session_begin','upper_extremity',
'pain_extremity','affected_segment','movement','metrics','time_movement','games',
'game_levels','iterations','errors_numbers_game','time_level_acomplished',
'patient_behavior','observations','date_session_end','period',]
class RehabilitationSessionUpdate(UpdateView):
model = RehabilitationSession
success_url = reverse_lazy('rehabilitationsessions:list')
fields = ['patient', 'medical', 'therapist', 'status', 'date_session_begin', 'upper_extremity',
'pain_extremity', 'affected_segment', 'movement', 'metrics', 'time_movement', 'games',
'game_levels', 'iterations', 'errors_numbers_game', 'time_level_acomplished',
'patient_behavior', 'observations', 'date_session_end', 'period', ]
这是我的urls.py
项目主文件:
from django.conf.urls import url, include
urlpatterns = [
url(r'^sesiones-de-rehabilitacion/', include('medical_encounter_information.urls', namespace='rehabilitationsessions')),
# Call the medical_encounter_information/urls.py
]
这是我的medical_encounter_information/urls.py
from django.conf.urls import include, url, patterns
from .views import RehabilitationSessionCreate
urlpatterns = [
url(r'^$', RehabilitationSessionList.as_view(), name='list'),
url(r'^(?P<pk>\d+)$', RehabilitationSessionDetail.as_view(), name='detail'),
url(r'^nuevo/', RehabilitationSessionCreate.as_view(), name='new'),
url(r'^editar/(?P<pk>\d+)$', RehabilitationSessionUpdate.as_view(), name='edit'),
url(r'^borrar/(?P<pk>\d+)$', RehabilitationSessionDelete.as_view(), name='delete'),
]
在我的forms.py
文件中,我有:
class RehabilitationSessionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# user = kwargs.pop('user', None)
super(RehabilitationSessionForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', u'Save'))
def save(self, commit=True):
rehabilitation_session = super(RehabilitationSessionForm, self).save(commit=False)
patient = self.cleaned_data['patient']
if commit:
rehabilitation_session.save()
return rehabilitation_session
class Meta:
model = RehabilitationSession
widgets = {
'pain_extremity':forms.RadioSelect,
'upper_extremity':forms.RadioSelect
}
fields = '__all__'
模板medical_encounter_information/templates/medical_encounter_information/rehabilitationsession_form.html
是:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %}Crear Registro{% endblock %}
{% block content %}
<div>
{% crispy form %}
{% csrf_token %}
</div>
{% endblock %}
当我在浏览器中输入网址http://localhost:8000/sesiones-de-rehabilitacion/nuevo/
时,我会收到以下信息:
File "/home/bgarcial/.virtualenvs/nrhb_dev/lib/python3.5/site-packages/django/views/generic/edit.py", line 138, in get_form_class
"Specifying both 'fields' and 'form_class' is not permitted."
django.core.exceptions.ImproperlyConfigured: Specifying both 'fields' and 'form_class' is not permitted.
[11/Jul/2016 15:44:29] "GET /sesiones-de-rehabilitacion/nuevo/ HTTP/1.1" 500 100235
但是,当我在浏览器中输入网址http://localhost:8000/sesiones-de-rehabilitacion/editar/1
时,我会查看以下内容:
因为视图sesiones-de-rehabilitacion/editar/1
(RehabilitationSessionUpdate
)已重新定位,并且视图sesiones-de-rehabilitacion/nuevo/
(RehabilitationSessionCreate
)未重新定义,因为它们共同采用相同的模板?
答案 0 :(得分:4)
如错误所示,您无法为视图设置form_class
和fields
。您可以设置form_class
class RehabilitationSessionCreate(CreateView):
model = RehabilitationSession
form_class = RehabilitationSessionForm # Not you should *not* have ()
success_url = reverse_lazy('rehabilitationsessions:list')
或者您可以设置fields
:
class RehabilitationSessionCreate(CreateView):
model = RehabilitationSession
fields = ['patient', ...]
success_url = reverse_lazy('rehabilitationsessions:list')
您不需要同时设置fields
和form_class
,因为您可以在表单上设置fields
:
class RehabilitationSessionForm(forms.ModelForm):
class Meta:
fields = ['patient', ... ]