【问题标题】:How to pass the value of a list in Django to a method?如何将Django中列表的值传递给方法?
【发布时间】:2021-11-12 20:54:29
【问题描述】:

我正在尝试做这样的事情。我有一个带有li 项目的导航栏:

**index.html**
<ul class="submenu dropdown-menu">
 <li><a class="dropdown-item" name="mis" href="{% url 'pdfNotes'%}">MIS</a></li>
                          <li><a class="dropdown-item" href="{% url 'pdfNotes'%}">MA</a></li>
                          <li><a class="dropdown-item" href="{% url 'pdfNotes'%}">UXD</a></li>
                          <li><a class="dropdown-item" href="{% url 'pdfNotes'%}">OSS</a></li>
</ul>

当我导航到第一个列表时'

  • ',即对于MIS,我必须重定向到名称为'mis'的pdfNotes.html,以便我可以将其用作views.py中的参数来过滤我的数据并显示pdf 笔记中只有“MIS”的详细信息。对于所有其他 li 项目也是如此。
    **pdfNotes.html**
    {% if pdfnote %}
    
    
    <table>
    
        <tr>
            <th># </th>
            <th>NAME</th>
            <th>DOWNLOAD FILE</th>
        </tr>
        {% with counter=1 %}
        {% for item in pdfnote %} 
        {% with crs=item.course %}
        
        <tr>
            <td id="id">{{crs}}</td>
            <td id="id">{{pattn}}</td>
            <td id="id">{{sem}}</td>
            <td id="id">{{ forloop.counter}}</td>
            <td id="name">{{item.name}}</td>
            <td id="downloadBtn">
                <a href="{{item.file.url}}" class="btn-outline-success" download >DOWNLOAD</a>
            </td>    
        </tr>
        {% endwith %}
        {% endfor %}
        {% endwith %}
    </table>
    
    
    **model.pdf**
    
    class PDF_Notes(models.Model):
    
        name=models.CharField("File name",max_length=100)
        subject=models.CharField("Subject",max_length=50)
        course=models.CharField("Course",max_length=50)
        semester=models.CharField("Semister",max_length=50)
        year=models.CharField("Year",max_length=50)
        source=models.CharField("Source",max_length=100)
        file=models.FileField(upload_to="media/PdfNotes")
    
        def __str__(self):
            return self.name
    
    
    **view.py**
    
    def pdfNotes(request):
    
        pdfNotes_file=PDF_Notes.objects.all()
        #sub=request.GET[]
        if(request.GET['mis']):
            pdfNotes_file=PDF_Notes.objects.all().filter(subject="MIS")
        n=len(pdfNotes_file)
        print("hello",pdfNotes_file)
        params={'pdfnote':pdfNotes_file,'total_items':n}
        return render(request,'pdfNotes.html',params)
    

    我该怎么做?

  • 【问题讨论】:

      标签: python html django scripting jinja2


      【解决方案1】:

      我建议创建以下函数:

      def get_pdf_notes(request, subject):
          pdfNotes_file=PDF_Notes.objects.all().filter(subject=subject)
          n=len(pdfNotes_file)
          params={'pdfnote':pdfNotes_file,'total_items':n}
          return render(request,'pdfNotes.html',params)
      

      在此 get_pdf_notes 的 URL 中添加路径,名称为 'get_pdf_notes',然后修改 HTML 中的 url,如下所示将参数传递给函数:

      <ul class="submenu dropdown-menu">
       <li><a class="dropdown-item" name="mis" href="{% url 'get_pdf_notes MIS'%}">MIS</a></li>
       <li><a class="dropdown-item" href="{% url 'get_pdf_notes MA'%}">MA</a></li>
       <li><a class="dropdown-item" href="{% url 'get_pdf_notes UXD'%}">UXD</a></li>
       <li><a class="dropdown-item" href="{% url 'get_pdf_notes OSS'%}">OSS</a></li>
      </ul>
      

      【讨论】:

      • thanx,Ahmed,我明白了你的想法 :) 我尝试了这个,对 url.py 进行了一些改动,它可以工作。
      【解决方案2】:

      据我所知,您在 index.html 中使用相同的 href 构建所有 url 调用,因此在请求处理后很难细化。为什么不简单地在 url 中添加一个带有所需 pdf 部分的参数并检查它?

      例如:

      <li><a class="dropdown-item" name="mis" href="{% url 'pdfNotes'%}/mis">MIS</a></li>
      

      更改 urls.py 以接受此参数:

      path('pdfNotes/<str:pdf_section>', views.pdfNotes)
      

      并在views.py中处理该参数:

      pdf_section = requests.get['pdf_section']
      

      然后随心所欲地使用 pdf_section。

      我希望我没有正确地回答您的问题?希望对您有所帮助。

      【讨论】:

      • Thanx Branko :) 它完全符合您的建议。
      • 你能接受答案吗
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      相关资源
      最近更新 更多