在Django UpdateView和CreateView中使用通用视图后,尝试为我拥有的每个模型定义“创建,编辑和列出”页面。保存后,转发到列表视图时,出现HTTP错误405,但未保存任何数据。
我将Django UpdateView用于更新形式,将CreateView用于添加,将SingleTableView用于列表。
Models.py
class PartnerOrganization(TimeStampedModel):
name = models.CharField(
max_length=100,
unique=True,
verbose_name=_('Name'))
coordinator = models.ForeignKey(
Coordinator,
blank=False, null=True,
related_name='+',
verbose_name=_('Coordinator')
)
land_phone_number = models.CharField(
max_length=100,
blank=True, null=True,
verbose_name=_('Partner land phone number')
)
fax_number = models.CharField(
max_length=100,
blank=True, null=True,
verbose_name=_('Partner fax number')
)
email = models.CharField(
max_length=100,
blank=True, null=True,
verbose_name=_('Partner email')
)
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=False, null=True,
related_name='+',
verbose_name=_('Created by')
)
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
def get_absolute_url(self):
return reverse("learningcenters:teacher_list")
在Views.py
class ListingPartnerView(LoginRequiredMixin,
GroupRequiredMixin,
FilterView,
ExportMixin,
SingleTableView,
RequestConfig):
table_class = PartnerTable
model = PartnerOrganization
template_name = 'learningcenters/partner/list.html'
table = BootstrapTable(PartnerOrganization.objects.all(), order_by='id')
# filterset_class = TeacherFilter
group_required = [u"UNICEF"]
def get_queryset(self):
force_default_language(self.request)
if self.request.user.groups.filter(name__in=['UNICEF']).exists():
return PartnerOrganization.objects.all()
class AddPartnerView(LoginRequiredMixin,
GroupRequiredMixin,
CreateView):
template_name = 'bootstrap4/common_form.html'
form_class = PartnerForm
queryset = PartnerOrganization.objects.all()
# success_url = '/learningcenters/teacher/list/'
group_required = [u"UNICEF"]
def get_absolute_url(self):
# return reverse("learningcenters:teacher_update", kwargs={"pk": self.id})
return reverse("learningcenters:partner_list")
def form_valid(self, form):
print(form.cleaned_data)
form.save(self.request)
return super(AddPartnerView, self).form_valid(form)
class UpdatePartnerView(LoginRequiredMixin,
GroupRequiredMixin,
UpdateView):
template_name = 'bootstrap4/common_form.html'
form_class = PartnerForm
queryset = PartnerOrganization.objects.all()
# success_url = '/learningcenters/teacher/list/'
group_required = [u"UNICEF"]
def get_object(self):
id_ = self.kwargs.get("pk")
return get_object_or_404(PartnerOrganization, id=id_)
def get_absolute_url(self):
return reverse("learningcenters:partner_list")
def form_valid(self, form):
print(form.cleaned_data)
form.save(self.request)
return super(UpdatePartnerView, self).form_valid(form)
Tables.py
class PartnerTable(tables.Table):
edit_column = tables.TemplateColumn(verbose_name=_('Edit partner'), orderable=False,
template_name='learningcenters/partner/edit_column.html',
attrs={'url': '/learningcenters/partner/update/'})
# delete_column = tables.TemplateColumn(verbose_name=_('Delete learning center'), orderable=False,
# template_name='learningcenters/delete_column.html',
# attrs={'url': '/api/learningcenters/'})
# edit = tables.LinkColumn('learningcenters:edit', text='Edit', args=[A('pk')])
class Meta:
model = PartnerOrganization
template = 'django_tables2/bootstrap.html'
fields = (
'edit_column',
# 'delete_column',
'name',
'email',
'land_phone_number',
'fax_number',
)
Forms.py
class PartnerForm(forms.ModelForm):
name = forms.CharField(
label=_("Partner name"),
widget=forms.TextInput, required=True
)
email = forms.EmailField(
label=_('Partner email'),
widget=forms.TextInput(attrs={'placeholder': 'Format: partner@email.com'})
)
land_phone_number = forms.RegexField(
label=_('Partner land phone number'),
regex=r'^[0-9]{2}-[0-9]{6}$',
widget=forms.TextInput(attrs={'placeholder': 'Format: 00-00000'})
)
fax_number = forms.RegexField(
label=_('Partner fax number'),
regex=r'^[0-9]{2}-[0-9]{6}$',
required=False,
widget=forms.TextInput(attrs={'placeholder': 'Format: 00-00000'})
)
coordinator_name = forms.CharField(
label=_("Coordinator name"),
widget=forms.TextInput, required=True
)
coordinator_email = forms.EmailField(
label=_('Coordinator email'),
widget=forms.TextInput(attrs={'placeholder': 'Format: partner@email.com'})
)
coordinator_land_phone_number = forms.RegexField(
label=_('Coordinator land phone number'),
regex=r'^[0-9]{2}-[0-9]{6}$',
widget=forms.TextInput(attrs={'placeholder': 'Format: 00-00000'})
)
coordinator_fax_number = forms.RegexField(
label=_('Coordinator fax number'),
regex=r'^[0-9]{2}-[0-9]{6}$',
required=False,
widget=forms.TextInput(attrs={'placeholder': 'Format: 00-00000'})
)
def __init__(self, *args, **kwargs):
super(PartnerForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = True
self.helper.form_action = reverse('learningcenters:partner_list', kwargs={})
self.helper.layout = Layout(
Fieldset(
None,
Div(
HTML('<h4 id="alternatives-to-hidden-labels">' + _('Basic Info') + '</h4>')
),
Div(
HTML('<span class="badge badge-default">1</span>'),
Div('name', css_class='col-md-3'),
),
css_class='bd-callout bd-callout-primary'
),
Fieldset(
None,
Div(
HTML('<h4 id="alternatives-to-hidden-labels">' + _('Partner Contact') + '</h4>')
),
Div(
HTML('<span class="badge badge-default">2</span>'),
Div('land_phone_number', css_class='col-md-3'),
HTML('<span class="badge badge-default">3</span>'),
Div('fax_number', css_class='col-md-3'),
HTML('<span class="badge badge-default">4</span>'),
Div('email', css_class='col-md-3'),
css_class='row',
),
css_class='bd-callout bd-callout-primary'
),
Fieldset(
None,
Div(
HTML('<h4 id="alternatives-to-hidden-labels">' + _('Coordinator Contact') + '</h4>')
),
Div(
HTML('<span class="badge badge-default">5</span>'),
Div('coordinator_name', css_class='col-md-3'),
css_class='row',
),
Div(
HTML('<span class="badge badge-default">6</span>'),
Div('coordinator_email', css_class='col-md-3'),
HTML('<span class="badge badge-default">7</span>'),
Div('coordinator_land_phone_number', css_class='col-md-3'),
HTML('<span class="badge badge-default">8</span>'),
Div('coordinator_fax_number', css_class='col-md-3'),
css_class='row',
),
css_class='bd-callout bd-callout-primary'
),
FormActions(
Submit('save', _('Save'), css_class='child_data'),
Submit('save_add_another', _('Save and add another'), css_class='child_data'),
# Submit('save_continue_editing', _('Save and continue editing')),
HTML('<a class="btn btn-info cancel-button" href="/learningcenters/partner/list/" translation="' +
_('Are you sure you want to cancel?') + '">' + _('Back to list') + '</a>'),
)
)
class Meta:
model = PartnerOrganization
fields = (
'name',
'email',
'land_phone_number',
'fax_number',
'coordinator_name',
'coordinator_email',
'coordinator_land_phone_number',
'coordinator_fax_number',
# 'bln_round',
# 'rs_round',
# 'cbece_round',
)
initial_fields = fields
widgets = {}
class Media:
js = ()
urls.py
url(
regex=r'partner/list/$',
view=views.ListingPartnerView.as_view(),
name='partner_list'
),
url(
regex=r'^partner/add/$',
view=views.AddPartnerView.as_view(),
name='partner_add'
),
url(
regex=r'^partner/update/(?P<pk>[\w.@+-]+)/$',
view=views.UpdatePartnerView.as_view(),
name='partner_update'
),
HTTP错误405 不允许的方法(POST):/ learningcenters / partner / list /