Django:将表单从一个应用程序添加到另一个应用程序中';s应用程序模板(详细视图)
我想为用户添加一个表单,以添加评级和评论,从而从另一个应用程序(评级应用程序和项目应用程序)更新模型。 我已经为此挣扎了一段时间。 现在我有: 评级模型.pyDjango:将表单从一个应用程序添加到另一个应用程序中';s应用程序模板(详细视图),django,django-forms,Django,Django Forms,我想为用户添加一个表单,以添加评级和评论,从而从另一个应用程序(评级应用程序和项目应用程序)更新模型。 我已经为此挣扎了一段时间。 现在我有: 评级模型.py class Avaliacao(models.Model): RATING_CHOICES = ( (1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'), ) created_at = mod
class Avaliacao(models.Model):
RATING_CHOICES = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)
created_at = models.DateTimeField(auto_now=True)
usuario = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
safra = models.ForeignKey(Safra,blank=False,on_delete=models.SET_NULL,null=True)
nota = models.IntegerField(choices=RATING_CHOICES)
comment = models.TextField()
class NotaFormulario(ModelForm):
class Meta:
model = Avaliacao
fields = ['nota','comment']
urlpatterns = [
url(r'^safra/(?P<safra_id>[0-9]+)/add_review/$',views.add_review, name='add_review')
]
def add_review(request, safra_id):
safra = get_object_or_404(Safra, pk=safra_id)
form = NotaFormulario(request.POST)
if form.is_valid():
nota = form.cleaned_data['nota']
comment = form.cleaned_data['comment']
usuario = request.user
avaliacao = Avaliacao()
avaliacao.safra = safra
avaliacao.usuario = usuario
avaliacao.nota = nota
avaliacao.comment = comment
avaliacao.created_at = datetime.datetime.now()
avaliacao.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('vinhos:safra_detalhes', args=(safra.id,)))
return render(request, 'vinhos/safra_detail.html', {'safra': safra, 'formulario': form})
forms.py
class Avaliacao(models.Model):
RATING_CHOICES = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)
created_at = models.DateTimeField(auto_now=True)
usuario = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
safra = models.ForeignKey(Safra,blank=False,on_delete=models.SET_NULL,null=True)
nota = models.IntegerField(choices=RATING_CHOICES)
comment = models.TextField()
class NotaFormulario(ModelForm):
class Meta:
model = Avaliacao
fields = ['nota','comment']
urlpatterns = [
url(r'^safra/(?P<safra_id>[0-9]+)/add_review/$',views.add_review, name='add_review')
]
def add_review(request, safra_id):
safra = get_object_or_404(Safra, pk=safra_id)
form = NotaFormulario(request.POST)
if form.is_valid():
nota = form.cleaned_data['nota']
comment = form.cleaned_data['comment']
usuario = request.user
avaliacao = Avaliacao()
avaliacao.safra = safra
avaliacao.usuario = usuario
avaliacao.nota = nota
avaliacao.comment = comment
avaliacao.created_at = datetime.datetime.now()
avaliacao.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('vinhos:safra_detalhes', args=(safra.id,)))
return render(request, 'vinhos/safra_detail.html', {'safra': safra, 'formulario': form})
url.py
class Avaliacao(models.Model):
RATING_CHOICES = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)
created_at = models.DateTimeField(auto_now=True)
usuario = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
safra = models.ForeignKey(Safra,blank=False,on_delete=models.SET_NULL,null=True)
nota = models.IntegerField(choices=RATING_CHOICES)
comment = models.TextField()
class NotaFormulario(ModelForm):
class Meta:
model = Avaliacao
fields = ['nota','comment']
urlpatterns = [
url(r'^safra/(?P<safra_id>[0-9]+)/add_review/$',views.add_review, name='add_review')
]
def add_review(request, safra_id):
safra = get_object_or_404(Safra, pk=safra_id)
form = NotaFormulario(request.POST)
if form.is_valid():
nota = form.cleaned_data['nota']
comment = form.cleaned_data['comment']
usuario = request.user
avaliacao = Avaliacao()
avaliacao.safra = safra
avaliacao.usuario = usuario
avaliacao.nota = nota
avaliacao.comment = comment
avaliacao.created_at = datetime.datetime.now()
avaliacao.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('vinhos:safra_detalhes', args=(safra.id,)))
return render(request, 'vinhos/safra_detail.html', {'safra': safra, 'formulario': form})
template.html(这是另一个应用程序中的我的详细信息模板)
这些字段未渲染。HTML如下所示:
<form action="/safra/6/add_review/" method="post" >
<input type="hidden" name="csrfmiddlewaretoken" value="ofbt2lvghvsNhPuSVRDtAgP3gYoGI55AIoXcA8EpUE29Sp">
<input type="submit" value="Enviar" class="btn btn-primary btn-large">
</form>
表单仅在我点击send一次后呈现,然后呈现“此字段是必需的”警告
总的来说,我一直在Django中使用表单,因为在我尝试开发的应用程序上,表单总是有这样一个横截面:从一个应用程序在模型上创建实例,从另一个应用程序的另一个模型在细节视图上创建实例。也尝试在GET方法中传递表单,并检查是否看到它。它不起作用。我认为这是关于向细节视图添加上下文的,但当我尝试在细节视图上使用get_context_数据时,我遇到了一系列新的错误。只需尝试在get方法中传递表单,并检查是否看到它。它不起作用。我认为这是关于向细节视图添加上下文的,但是当我尝试在细节视图上使用get_context_数据时,我遇到了一系列新的错误。