所以我一直在寻找一个解决方案约一个星期,现在我只需要从一个表单内的隐藏标签的数据女巫应该没有问题,但不是从标签的数据,我只是得到'NONE'
模板(形式):
{% for Gerecht in Gerechten %}
<form method="POST">
{% csrf_token %}
</section>
<section class="u-clearfix u-custom-color-2 u-valign-middle u-section-2" id="sec-a9e5">
<div class="u-clearfix u-gutter-0 u-layout-wrap u-layout-wrap-1">
<div class="u-layout" style="">
<div class="u-layout-row" style="">
<div class="u-align-left u-container-style u-image u-layout-cell u-left-cell u-shading u-size-30 u-size-xs-60 u-image-1" src="" data-image-width="4880" data-image-height="3253" style=" background-image: linear-gradient(0deg, rgba(0,0,0,0.25), rgba(0,0,0,0.25)), url('{% static '/img/{{ Gerecht.afbeelding }}' %}')">
<div class="u-container-layout u-container-layout-1" src=""></div>
</div>
<div class="u-align-left u-container-style u-custom-color-1 u-layout-cell u-right-cell u-size-30 u-size-xs-60 u-layout-cell-2">
<div class="u-container-layout u-container-layout-2">
<h2 class="u-text u-text-default u-text-white u-text-1"> {{ Gerecht.naam }}</h2>
<p class="u-text u-text-default u-text-white u-text-2">Duur: {{ Gerecht.duur }} u </p>
<input type="submit" value="ga naar gerecht" class="u-active-custom-color-3 u-align-left u-border-1 u-border-active-custom-color-3 u-border-hover-custom-color-3 u-border-white u-btn u-btn-round u-button-style u-hover-custom-color-3 u-none u-radius-12 u-text-active-white u-text-hover-white u-btn-2" name="btnZieRecept">
<label name="id" style="display: none;" value="{{ Gerecht.id }}">{{ Gerecht.id }}</label>
</div>
</div>
</div>
</div>
</div>
</section>
</form>
{% endfor %}
我views.py:
if request.POST.get('btnZieRecept'):
PK = request.POST.get('id')
logging.basicConfig(level=logging.NOTSET)
logging.debug(request.POST.get('id'))
return redirect('GerechtPreview', PK=PK)
所以这里PK=None,我不知道为什么
def GerechtPreview(request, PK):
obj = Gerecht.objects.get(pk=PK)
ingList=[]
for f in obj.bijIngridiënten:
ingList.append(f)
for f in obj.Hoofdingridiënten:
ingList.append(f)
return render(request, 'GerechtPreview.html', {"obj":obj, "ingList":ingList})
我的urls . py:
path('Gerechten.html', views.Gerechten, name="Gerechten.html")
re_path(r'^GerechtPreview.html/(?P<PK>w+)/$', views.GerechtPreview, name="GerechtPreview"),
有人有什么想法吗?
- 看起来你没有在模板中使用任何Django表单。如果您的表单是基于模型的,我将传递"modelform"; 表单中的隐藏字段通常是这样的:
<input type="hidden" name="csrfmiddlewaretoken" value="HVjtndYO...1DIkUex7">
如果你检查HTML(因为{% csrf_token %}
)
,你应该能够在你当前的表单上看到一个例子。无论如何我不是HTML专家;我从来没有见过通过<label>
标签做过,但也许你可以。我只是建议用<input type="hidden"
尝试它,并给它一个名称和一个值,看看它是否在POST
请求中通过。