Python 基于类的视图使用“过滤”;def get_queryset(自我):";-协助解决问题

Python 基于类的视图使用“过滤”;def get_queryset(自我):";-协助解决问题,python,python-3.x,django,Python,Python 3.x,Django,我正在尝试过滤我的Web应用程序中的数据,但在使其正常工作时遇到了问题。 我的应用程序有一个用户模型,我为其分配了不同的程序。每个程序包含不同的练习 程序模型: class Program(models.Model): patient = models.ForeignKey(User, on_delete=models.CASCADE, default=0) program_name = models.CharField(max_length=1000, default=&quo

我正在尝试过滤我的Web应用程序中的数据,但在使其正常工作时遇到了问题。 我的应用程序有一个用户模型,我为其分配了不同的程序。每个程序包含不同的练习

程序模型:

class Program(models.Model):
    patient = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
    program_name = models.CharField(max_length=1000, default="")
    date_posted = models.DateTimeField(default=timezone.now)
程序视图:

class ProgramListView(ListView):
    model = Program
    template_name = 'program/prog.html' 
    context_object_name = 'programs'

    def get_queryset(self):
        return Program.objects.filter(patient=self.request.user)
程序模板:

{% extends "program/base.html" %}
{% block content %}
    <h3> Please Choose your Program according to your Plan</h3>
    {% for program in programs %}
        <article class="media content-section">
            <div class="media-body">
                <div class="article-metadata">
                    <a class="mr-2" href="{% url 'program-detail' program.id %}">Program Name - {{ program.program_name }}</a>
                </div>
                    <p class="article-content">Date Posted - {{ program.date_posted }}</p>
                </div>
        </article>
    {% endfor %}
{% endblock content %}

{% extends "program/base.html" %}
{% block content %}
    <h3> Program Exercises List</h3>
    {% for exercise in exercises %}

        <h3>{{ exercise.name }}</h3>
        <h3>{{ exercise.description }}</h3>

    {% endfor %}
{% endblock content %}
练习视图(ProgramDetailView):

练习模板:

{% extends "program/base.html" %}
{% block content %}
    <h3> Please Choose your Program according to your Plan</h3>
    {% for program in programs %}
        <article class="media content-section">
            <div class="media-body">
                <div class="article-metadata">
                    <a class="mr-2" href="{% url 'program-detail' program.id %}">Program Name - {{ program.program_name }}</a>
                </div>
                    <p class="article-content">Date Posted - {{ program.date_posted }}</p>
                </div>
        </article>
    {% endfor %}
{% endblock content %}

{% extends "program/base.html" %}
{% block content %}
    <h3> Program Exercises List</h3>
    {% for exercise in exercises %}

        <h3>{{ exercise.name }}</h3>
        <h3>{{ exercise.description }}</h3>

    {% endfor %}
{% endblock content %}
我遇到的问题是:

  • 只有“pk=1”起作用(只有列表中的第一个程序),但它显示了这个错误-
  • 似乎他没有得到数据库中所有的练习,它不能使用“For”循环

    2.当我尝试列表中的其他程序时,我得到404错误-

    Page not found (404)
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/programs/2/
    Raised by:  program.views.ProgramDetailView
    No exercise found matching the query
    
    You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
    
    在SQLite中,我确实有与“pk=2”相关的练习,因此我假设列表中的练习显示了与“pk=1”相同的错误,但他似乎无法在数据库中找到练习

    感谢你的帮助


    谢谢,我希望我没有错过任何“代码行”。

    您有model=Exercise用于class programmadetailview。因此,您在该视图中显示了多个练习,这表明它应该是
    列表视图
    ,而不是
    详细视图
    。另一种方法是使用带有
    model=Program
    DetailView
    ,然后使用
    {%for exercise in Program.exercise\u set.all%}
    循环相关的练习。我将该类更改为ListView,现在它工作正常了。谢谢你的帮助!
    get_object_or_404
    
    TypeError at /programs/1/
    'Exercise' object is not iterable
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/programs/1/
    Django Version: 3.1
    Exception Type: TypeError
    Exception Value:    
    'Exercise' object is not iterable
    Exception Location: C:\Users\shai3\Python\lib\site-packages\django\template\defaulttags.py, line 167, in render
    Python Executable:  C:\Users\shai3\Python\python.exe
    Python Version: 3.8.5
    Python Path:    
    ['C:\\Users\\shai3\\PycharmProjects\\website',
     'C:\\Users\\shai3\\Python\\python38.zip',
     'C:\\Users\\shai3\\Python\\DLLs',
     'C:\\Users\\shai3\\Python\\lib',
     'C:\\Users\\shai3\\Python',
     'C:\\Users\\shai3\\Python\\lib\\site-packages']
    Server time:    Thu, 03 Sep 2020 12:04:29 +0300
    
    Page not found (404)
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/programs/2/
    Raised by:  program.views.ProgramDetailView
    No exercise found matching the query
    
    You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.