gpt4 book ai didi

python - 不允许同时指定 'fields' 和 'form_class'

转载 作者:太空宇宙 更新时间:2023-11-04 08:48:02 25 4
gpt4 key购买 nike

我有以下表单,我想用 Django 脆皮表单呈现它。

这是我的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 %}

当我在浏览器中输入 url 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

但是,当我在浏览器中输入 url http://localhost:8000/sesiones-de-rehabilitacion/editar/1 时,我会看到以下内容:

enter image description here

因为 View ​​sesiones-de-rehabilitacion/editar/1 (RehabilitationSessionUpdate) 被渲染,并且 View sesiones-de-rehabilitacion/nuevo/ (RehabilitationSessionCreate) 没有被渲染知道一起使用相同的模板?

最佳答案

如错误所述,您不能同时为您的 View 设置 form_classfields。您可以设置 form_class

class RehabilitationSessionCreate(CreateView):
model = RehabilitationSession
form_class = RehabilitationSessionForm # Note you should *not* have ()
success_url = reverse_lazy('rehabilitationsessions:list')

或者你可以设置字段:

class RehabilitationSessionCreate(CreateView):
model = RehabilitationSession
fields = ['patient', ...]
success_url = reverse_lazy('rehabilitationsessions:list')

您不需要同时设置 fieldsform_class,因为您可以在表单本身上设置 fields:

class RehabilitationSessionForm(forms.ModelForm):
class Meta:
fields = ['patient', ... ]

关于python - 不允许同时指定 'fields' 和 'form_class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com