【发布时间】:2021-09-25 22:14:30
【问题描述】:
我正在为 section.html 使用基于类的 d 视图,在该视图中,我尝试借助表单实现添加到购物车功能,但在单击添加到汽车后却显示错误
未找到没有参数的“部分”的反向。尝试了 1 种模式:['(?P
这是我的意见.py
@method_decorator(login_required, name='dispatch')
class Sections(View):
def post(self, request, subject_id):
sections =request.POST.get('section')
print(sections)
return redirect('subjects:section')
def get(self, request, subject_id):
subject = get_object_or_404(Subject, pk=subject_id) # retrieve the subject
sections = subject.section.all() # get the sections related to the subject
return render (request, 'sections.html',{"section_list" : sections })
我的 urls.py
urlpatterns = [
path('<int:subject_id>/Sections/', Sections.as_view(),name='section'),
]
我的section.html
<ul class="sec">
{% for section in section_list %}
<div class="card col-11">
<div class="card-header">
{{ section.title }}
</div>
<div class="card-body">
<h5 class="card-about">{{ section.about_section }}</h5> <br>
<i class="price fas fa-rupee-sign"> {{ section.price }}</i> <br> <br>
<i class="icon text-white fas fa-chalkboard-teacher"> {{ section.teacher }}</i>
<i class="icon text-white fas fa-clock"> {{ section.content_duration}}</i>
<i class="icon text-white fas fa-tags"> {{ section.subject.name }}</i>
<form action="#" method="POST">
{% csrf_token %}
<input hidden type="text" name="section" value="{{section.id}}">
<input type="submit" class="btn btn-outline-primary" value="Add To Cart">
</form>
</div>
</div>
{% endfor %}
</ul>
我的部分模型与另一个带有外键的模型名称主题相关
【问题讨论】:
标签: django django-models django-forms django-templates django-urls