匹配的查询不存在?

发布于 2024-11-18 10:34:16 字数 3661 浏览 0 评论 0原文

这是视图:

def showProject(request, project_slug):
project = Project.objects.get(slug=project_slug)
tickets = Ticket.objects.filter(project=project)
payload = { 'project':project, 'tickets':tickets }
return render(request, 'project/project.html', payload)

这是错误:

Traceback:
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\\project\views.py" in showProject
  13.   project = Project.objects.get(slug=project_slug)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\query.py" in get
  349.                     % self.model._meta.object_name)

Exception Type: DoesNotExist at /project/ticket/

Exception Value: Project matching query does not exist.

对预期内容的更详细解释: 我有一个侧边栏,列出了所有打开的“票证”。当我点击其中一张票时,它应该打开它。相反,当我尝试打开它时,我收到此错误。怎么了? 这是模型类:

class Project(models.Model):
    """simple project for tracking purposes"""
    name = models.CharField(max_length = 64)
    slug = models.SlugField(max_length = 100, unique=True,blank=True, null=True)
    description = models.CharField(max_length = 255)
    owner = models.ForeignKey(User, related_name="+")
    created_on = models.DateTimeField(auto_now_add = 1)
    active = models.BooleanField(default=True)
    parent = models.ForeignKey("self", related_name="children", null=True, blank=True)
    repository = models.ForeignKey("Repository", related_name="projects", null=True, blank=True)
    book = models.ForeignKey(Book, related_name="+", null=True, blank=True)
    acl = models.ManyToManyField(AclEntry)
    def save (self):
        if not self.slug:
            self.slug = '-'.join(self.name.lower().split())
        if not self.book:
            book = Book(name=self.name, owner=self.owner)
            book.save()
            self.book = book
        super(Project, self).save()

这是模板代码:

{% block title %}Tickets: {{project.name}}{% endblock %}

{% block main %}

<div id="project-nav">
<span><a href="/project/{{project.slug}}/">Tickets</a></span>
<span><a href="/book/{{book.slug}}{{book.name}}">Docs</a></span>
<span><a href="/project/{{project.slug}}/browse">Browser</a></span>
</div>
<div id="action-nav">
{% block actions %}
<span><a href="/project/{{project.slug}}/tickets/create">Create Ticket</a></span>
<span><a href="/project/{{ project.slug }}/tickets/recent">Recent           Activity</a>      </span>
<span><a href="/project/{{ project.slug }}/tickets/my/">My Tickets</a></span>
{% endblock %}
</div>
{% for ticket in tickets %}

<div class="ticket">
<div class="ticket-header">
    <div class="ticket-title">
        <a href="/project/ticket/{{ticket.pk}}">{{ticket.subject}}</a>
    </div>
    <div id="ticket-number">
        #{{ticket.pk}}
    </div>
    <div id="ticket-state">
        {{ticket.get_state_display}}
    </div>
    <div id="ticket-info">
        Reported by {{ticket.created_by}} | created: {{ticket.created_on   }} | modified: {{ticket.modified_on}}
    </div>
</div>
</div>
{% endfor %}
</div>

{% endblock %}

似乎我尝试做的一切都是回到这个,我不明白为什么。我缺少什么?非常感谢!

This is the view:

def showProject(request, project_slug):
project = Project.objects.get(slug=project_slug)
tickets = Ticket.objects.filter(project=project)
payload = { 'project':project, 'tickets':tickets }
return render(request, 'project/project.html', payload)

This is the error:

Traceback:
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\\project\views.py" in showProject
  13.   project = Project.objects.get(slug=project_slug)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\query.py" in get
  349.                     % self.model._meta.object_name)

Exception Type: DoesNotExist at /project/ticket/

Exception Value: Project matching query does not exist.

A more detailed explanation of what is expected: I have a sidebar that lists all open "tickets." When I click on one of those tickets, it should open it. Instead when I try to open it, I'm getting this error. What is happening?
Here is the model class:

class Project(models.Model):
    """simple project for tracking purposes"""
    name = models.CharField(max_length = 64)
    slug = models.SlugField(max_length = 100, unique=True,blank=True, null=True)
    description = models.CharField(max_length = 255)
    owner = models.ForeignKey(User, related_name="+")
    created_on = models.DateTimeField(auto_now_add = 1)
    active = models.BooleanField(default=True)
    parent = models.ForeignKey("self", related_name="children", null=True, blank=True)
    repository = models.ForeignKey("Repository", related_name="projects", null=True, blank=True)
    book = models.ForeignKey(Book, related_name="+", null=True, blank=True)
    acl = models.ManyToManyField(AclEntry)
    def save (self):
        if not self.slug:
            self.slug = '-'.join(self.name.lower().split())
        if not self.book:
            book = Book(name=self.name, owner=self.owner)
            book.save()
            self.book = book
        super(Project, self).save()

Here is the template code:

{% block title %}Tickets: {{project.name}}{% endblock %}

{% block main %}

<div id="project-nav">
<span><a href="/project/{{project.slug}}/">Tickets</a></span>
<span><a href="/book/{{book.slug}}{{book.name}}">Docs</a></span>
<span><a href="/project/{{project.slug}}/browse">Browser</a></span>
</div>
<div id="action-nav">
{% block actions %}
<span><a href="/project/{{project.slug}}/tickets/create">Create Ticket</a></span>
<span><a href="/project/{{ project.slug }}/tickets/recent">Recent           Activity</a>      </span>
<span><a href="/project/{{ project.slug }}/tickets/my/">My Tickets</a></span>
{% endblock %}
</div>
{% for ticket in tickets %}

<div class="ticket">
<div class="ticket-header">
    <div class="ticket-title">
        <a href="/project/ticket/{{ticket.pk}}">{{ticket.subject}}</a>
    </div>
    <div id="ticket-number">
        #{{ticket.pk}}
    </div>
    <div id="ticket-state">
        {{ticket.get_state_display}}
    </div>
    <div id="ticket-info">
        Reported by {{ticket.created_by}} | created: {{ticket.created_on   }} | modified: {{ticket.modified_on}}
    </div>
</div>
</div>
{% endfor %}
</div>

{% endblock %}

It seems that everything I try to do is going back to this, and I don't understand why. What am I missing? Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2024-11-25 10:34:16

它表示不存在 Project 对象,其 slug 等于 project_slug

您应该捕获如下异常

try:
    Project.objects.get(slug=project_slug)
except Project.DoesNotExist:
    raise sth...

It says there is no Project object whose slug is equal to project_slug.

You should catch the exception as below

try:
    Project.objects.get(slug=project_slug)
except Project.DoesNotExist:
    raise sth...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文