简体   繁体   English

如何在不更改模型的情况下解决 django 中的这个问题?

[英]How can I solve this issue in django without changing the models?

Info信息

  • I have two simple models in Django, with one-to-one relationship我在 Django 中有两个简单的模型,具有一对一的关系
  • I'm using generic views我正在使用通用视图
  • There are Issue s and Solutions s in the database, numbered 1 through 10数据库中有IssueSolutions ,编号为 1 到 10
  • Loading the Issue via the DetailView (eg localhost:8000/myapp/6/ ) works great通过DetailView加载Issue (例如localhost:8000/myapp/6/ )效果很好

Error错误

When trying to load the Solution view in a browser (eg localhost:8000/myapp/6/solution/ ), I get Page not found (404), No solution found matching the query .当尝试在浏览器中加载Solution视图时(例如localhost:8000/myapp/6/solution/ ),我得到Page not found (404), No solution found matching the query

Code代码

models.py :模型.py

class Issue(models.Model):
    def __str__(self):
        return self.issue_text
    issue_text = models.CharField(max_length=200)

class Solution(models.Model):
    def __str__(self):
        return self.solution_text
    issue = models.OneToOneField(Issue, on_delete=models.CASCADE)
    solution_text = models.CharField(max_length=200)

views.py :视图.py

class DetailView(generic.DetailView):
    model = Issue
    template_name = 'my_templates/detail.html'

class SolutionView(generic.DetailView):
    model = Solution
    template_name = 'my_templates/solution.html'

urls.py : urls.py

urlpatterns = [
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/solution/$', views.SolutionView.as_view(), name='solution'),
]

Question问题

I suspect that maybe the relationship between the models is incorrect - I can see that the view raises the 404 error because it can't find a solution object (though there are a few solution objects in the database, for every Issue ).我怀疑模型之间的关系可能不正确 - 我可以看到视图引发 404 错误,因为它找不到solution object (尽管数据库中有一些solution对象,对于每个Issue )。

I've been going through Django's docs on generic views and making Django queries to the database but I think I'm confusing the two.我一直在浏览Django 的通用视图文档并对数据库进行 Django 查询,但我认为我混淆了两者。

Also, debugging with pdb just makes the browser lose the object for some reason.此外,使用pdb进行调试只会使浏览器由于某种原因丢失 object。

Did I get the one-to-one relationship wrong?我把一对一的关系弄错了吗?

Which Django Version did you use?您使用的是哪个 Django 版本? try this...尝试这个...

urls.py网址.py

urlpatterns = [
    path('solution/<int:pk>/', SolutionView.as_view(), name='solution'),
]

views.py视图.py

class SolutionView(DetailView):
    model = Solution
    template_name ="my_templates/solution.html"


    def get_object(self):
        some_pk = self.kwargs.get("pk")
        return get_object_or_404(Solution, pk=some_pk)

Tested.经测试。 This works for me fine.这对我来说很好。 It´s django 3.0 but i guess down to version 2.x this should also work.它是 django 3.0 但我猜到版本 2.x 这也应该可以工作。

you have to send the integer variable from your request to your class based view some_pk so that django can get the right object.您必须将 integer 变量从您的请求发送到您的基于 class 的视图some_pk以便 django 可以获得正确的 ZA8CFDE6331BD4BEB66ACZ.6331BD4BEB66ACZ.69111

page not found relates also to your template path - so check it. page not found也与您的模板路径有关 - 请检查它。

Don´t forget to set the right template_name and to import everything.不要忘记设置正确的template_name名称并导入所有内容。

There was a mismatch between the app name and the object instance.应用名称与 object 实例不匹配。

In this case, the app, the module and the model objects were named differently.在这种情况下,应用程序、模块和 model 对象的名称不同。 For example - following django's tutorial : the name of the app should be polls and the templates sub-directory should also be polls/templates/polls , but in this case the app was named something like polls_app and the templates sub-directory something like polls_templates .例如 - 按照django 的教程:应用程序的名称应该是polls并且模板子目录也应该是polls/templates/polls ,但在这种情况下,应用程序被命名为polls_app和模板子目录类似于polls_templates . Any other naming mismatch results in the same way.任何其他命名不匹配都会以相同的方式产生。

I found this while trying to run tests in django - even though everything else worked (other than this specific, generic view), the tests failed with an error.我在尝试在 django 中运行测试时发现了这一点——即使其他一切正常(除了这个特定的通用视图),测试失败并出现错误。 Investigating that error led me to the tests runner code (see here or here ) and loadTestsFromName in it.调查该错误使我找到了测试runner程序代码(请参见此处此处)和其中的loadTestsFromName

So I guess that django relies on the object name (in the example above - it was searching for polls in polls_templates or something similar), but I couldn't find how this can be configured.所以我猜想 django 依赖于 object 名称(在上面的示例中 - 它在polls_templates或类似的东西中搜索polls ),但我找不到如何配置它。 Trying to debug it with pdb wans't great either, since I was getting way deep into django's source code.尝试用pdb调试它也不是很好,因为我已经深入了解 django 的源代码。

I created a new app, with everything named the same, and now tests are running ok, and also the SolutionView , so having everything called by the same name solved the question for me.我创建了一个新应用程序,所有名称都相同,现在测试运行正常,还有SolutionView ,所以用相同名称调用所有内容为我解决了这个问题。

I assume that there's a similar name-relying module in django's url which does the same thing.我假设在 django 的url中有一个类似的依赖名称的模块,它做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM