url和Django数据库找不到与具有相同工作类的查询匹配的对象

2024-09-30 10:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我搞不懂为什么OfertoHome视图有效,而OfertoListView不起作用,它们都是一样的-唯一不同的是在url中。在

class OfertoListView(ListView):
    model = Oferto
    paginate_by = 10
    template_name = "ofertoj/oferto_list.html"

def get_context_data(self,**kwargs):
    context = super(OfertoListView,self).get_context_data(**kwargs)
    return context


class OfertoHome(ListView):
    model = Oferto
    paginate_by = 10
    template_name = "ofertoj/oferto_list.html"

def get_context_data(self,**kwargs):
    context = super(OfertoHome,self).get_context_data(**kwargs)
    return context

在网址.py在

^{pr2}$

调试消息

   Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/oferto/listview/
No oferto found matching the query

这是我的全部视图.py在应用程序中,已发布,因为请求

    #!/usr/bin/env python
# -*- coding: utf-8 -*-
# ofertoj.views

from .models import Oferto
from django.views.generic import CreateView, UpdateView, DetailView
from django.views.generic import ListView

from turtle.views import Turtle_CreateView,ActionMixin,NameSearchMixin
from braces.views import LoginRequiredMixin

from forms import OfertoCreateForm,OfertoUpdateForm


class OfertoCreateView(LoginRequiredMixin,Turtle_CreateView):
    model = Oferto
    action = "created"
    form_class = OfertoCreateForm


class OfertoDetailView(DetailView):
    model = Oferto

class OfertoResultsView(OfertoDetailView):
    template_name = "Oferto/results.html"

class OfertoUpdateView(LoginRequiredMixin,ActionMixin,UpdateView):
    model = Oferto
    action = "created"
    form_class = OfertoUpdateForm

class OfertoListView(ListView):
    model = Oferto
    paginate_by = 10
    template_name = "ofertoj/oferto_list.html"

    def get_context_data(self,**kwargs):
        context = super(OfertoListView,self).get_context_data(**kwargs)
        return context


class OfertoHome(ListView):
    model = Oferto
    paginate_by = 10
    template_name = "ofertoj/oferto_list.html"

    def get_context_data(self,**kwargs):
        context = super(OfertoHome,self).get_context_data(**kwargs)
        return context

在网址.py在

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ofertoj/urls.py

from django.conf.urls import patterns, url
from views import *

urlpatterns = patterns("",
    url(
    regex=r"^createview/$",
    view=OfertoCreateView.as_view(),
    name="oferto_createview"
    ),


    url(
    regex=r"^(?P<slug>[-_\w]+)/$",
    view=OfertoDetailView.as_view(),
    name="oferto_detail"
    ),

    url(
    regex=r"^(?P<slug>[-_\w]+)/$/results/$",
    view=OfertoResultsView.as_view(),
    name="oferto_results"
    ),

    url(
    regex=r"^listview/$",
    view=OfertoListView.as_view(),
    name="oferto_listview"
    ),


    url(
    regex=r"^(?P<slug>[-_\w]+)/updateview/$",
    view=OfertoUpdateView.as_view(),
    name="oferto_updateview"
    ),

    url(
    regex=r"^$",
    view = OfertoHome.as_view(),
    name ="oferto_home"
    ),
)

Tags: namefromimportselfviewurldataget
1条回答
网友
1楼 · 发布于 2024-09-30 10:27:15

它可能会选择一个细节视图,并寻找一个名为listview的pk或slug的Oferto,但没有发现它产生404。请把你的全部网址.py找出冲突。在

是的,你的网址.py按顺序解析,第一个匹配的是已处理的视图。在您的例子中,oferto_detailurl中的r"^(?P<slug>[-_\w]+)/$"与url中的输入“listview”匹配。将listview/$url放在第一位,它应该可以正常工作。在

url(
regex=r"^(?P<slug>[-_\w]+)/$",
view=OfertoDetailView.as_view(),
name="oferto_detail"
),

url(
regex=r"^listview/$",
view=OfertoListView.as_view(),
name="oferto_listview"
),

相关问题 更多 >

    热门问题