【问题标题】:using class base view facing a issue of reverse not found使用面临未找到反向问题的类基视图
【发布时间】:2021-09-25 22:14:30
【问题描述】:

我正在为 section.html 使用基于类的 d 视图,在该视图中,我尝试借助表单实现添加到购物车功能,但在单击添加到汽车后却显示错误

未找到没有参数的“部分”的反向。尝试了 1 种模式:['(?P[0-9]+)/Sections/$']

这是我的意见.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


    【解决方案1】:

    您必须指定 subject_id 进行重定向,因为路径中有 &lt;int:subject_id&gt; 并且 get 方法需要它。

    @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', subject_id=subject_id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2012-01-25
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多