错误405 -使用Django将数据保存到自定义表单时不允许方法。Method Not Allowed (POST): /create-establishment/ Method Not Allowed: /create-establishment/
我创建了一个类,以及相关的视图和URL模式。HTML显示正常,我可以看到请求中的数据。然而,它就是不会通过。
型号:
class Establishments(models.Model):
org_name = models.CharField(max_length=255)
address = models.TextField()
epfo_code = models.CharField(max_length=20)
esic_code = models.CharField(max_length=15)
employer_name = models.CharField(max_length=60)
employer_phone = models.CharField(max_length=10)
contact_name = models.CharField(max_length=60)
contact_phone = models.CharField(max_length=10)
epfo_username = models.CharField(max_length=30)
epfo_password = models.CharField(max_length=30)
esic_username = models.CharField(max_length=30)
esic_password = models.CharField(max_length=30)
created_on = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = "Establishments"
def __str__(self):
return self.org_name
浏览次数:
``class CreateEstablishmentView(TemplateView):
def get(self, request):
form = CustomEstablishmentRegistrationForm()
return render(request, 'create_establishment.html', {'form': form})
class EstablishmentDetailView(TemplateView):
template_name = "establishment_detail.html"
def post(self, request):
form = CustomEstablishmentRegistrationForm(request.POST)
if form.is_valid():
establishment = form.save()
return redirect('establishment_detail', pk=establishment.pk)
return render(request, 'create_establishment.html', {'form': form})
def establishment_detail(request, pk):
establishment = Establishments.objects.get(pk=pk)
return render(request, 'establishment_detail.html', {'establishment': establishment})`
`
网址:
path("create-establishment/", views.CreateEstablishmentView.as_view(), name="create-establishment"),
path('establishment-detail/<int:pk>/', views.establishment_detail, name='establishment_detail'),
代码应在“机构”表中输入数据。
我尝试同时包含POST和GET方法,但错误仍然存在。我最初使用POST方法从表单中提取数据,但它看起来很笨拙,甚至不能工作。此外,我尝试使用基于函数的方法,但我有限的Django知识并没有带我去任何地方。
这个表工作得很好,我看到它是在DB中创建的。
def save_establishment(org_name, address, epfo_code, esic_code, employer_name, employer_phone, contact_name, contact_phone, epfo_username, epfo_password, esic_username, esic_password):
establishment = Establishments(
org_name=org_name,
address=address,
epfo_code=epfo_code,
esic_code=esic_code,
employer_name=employer_name,
employer_phone=employer_phone,
contact_name=contact_name,
contact_phone=contact_phone,
epfo_username=epfo_username,
epfo_password=epfo_password,
esic_username=esic_username,
esic_password=esic_password
)
establishment.save()
1条答案
按热度按时间6ovsh4lw1#
您的表单似乎正在POST到“CreateEstablishmentView”的URL。但是,该视图没有POST方法,因此出现错误。
看起来您需要做的就是将POST方法转移到CreateEstablishmentView类下面,这样POST方法就是该类的方法。