Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找页面出现问题。Django url/视图_Python_Django_Django Models_Django Views_Django Urls - Fatal编程技术网

Python 查找页面出现问题。Django url/视图

Python 查找页面出现问题。Django url/视图,python,django,django-models,django-views,django-urls,Python,Django,Django Models,Django Views,Django Urls,这是我第一次在StackOverflow问一些问题,我既兴奋又害怕,我不知道为什么。 我正在编写一个Django应用程序,它只托管web帖子。本页面分为三(3)类(索引、lkmi和chiqung)。每个帖子都有一个类别(lkmi或chiqung)。 在索引页上,你可以看到所有的帖子。 在LKMI页面上,您只能看到LKMI帖子。 在CHIQUNG页面上,您只能看到CHIQUNG帖子。 所有这些都由一个名为“index_page”的视图控制,该视图接收一个名为“cat”的参数,该参数是来自其中一个类

这是我第一次在StackOverflow问一些问题,我既兴奋又害怕,我不知道为什么。 我正在编写一个Django应用程序,它只托管web帖子。本页面分为三(3)类(索引、lkmi和chiqung)。每个帖子都有一个类别(lkmi或chiqung)。 在索引页上,你可以看到所有的帖子。 在LKMI页面上,您只能看到LKMI帖子。 在CHIQUNG页面上,您只能看到CHIQUNG帖子。 所有这些都由一个名为“index_page”的视图控制,该视图接收一个名为“cat”的参数,该参数是来自其中一个类别(index、lkmi、chiqung)的URL。基于这一点,它决定了加载哪些帖子

*现在问题来了*

我找不到原因,但我只是在加载lkmi部分时遇到问题。索引页面和chiqung_页面的加载非常完美,但我有一个

"Page Not Found (404)
 Request Method: GET
 Request URL:   http://127.0.0.1:8000/lkmi/
Django使用blog.URL中定义的URLconf,按以下顺序尝试了这些URL模式:

 admin/
 <cat> [name='index_page']
 post/<int:pk> [name='post_detallado']
 ^ckeditor/
职位

处理分类的视图

def index_page(request, cat):
    print('function index_page')
    print('cat ='+str(cat))
    category = get_object_or_404(Category,url=cat)
    print('get_object_or_404(Category,url=cat). Executed')
    print(str(category))
    postes = category.get_postes()
    print('category.get_postes(). Executed')
    return render(request, 'index.html',{'postes': postes,
                                         'category': category})
应用程序中的URL文件

urlpatterns = [
    path('admin/',admin.site.urls),
    path('<cat>', views.index_page, name='index_page'),
    path('post/<int:pk>', views.detailed_post, name='detailed_post'),
]
但如果我只输入“127.0.0.1:8000/无论什么”,错误只是

No Categoria matches the given query.
因此,在本例中,它输入了函数,并给出了404错误,即找不到匹配的类别。我仍然不明白,当我试图进入lkmiurl时,为什么它不执行视图

非常感谢你,我希望有人能帮我,这样我就不能继续了。

你的类被称为“Category”,但你在“Categoria”上调用
get\u object\u或\u 404()

视图应为:

def index_page(request, cat):
    categoria = get_object_or_404(Category,url=cat)
    postes = categoria.get_postes()
    return render(request, 'index.html',{'postes': postes,
                                         'categoria': categoria})

你的类被称为“Category”,但你在“Categoria”上调用
get\u object\u或\u 404()
“哦,对不起,那只是因为我用西班牙语编程,我把它改成了英语来问这个问题,这只是堆栈溢出问题,但谢谢你的注意。你100%确定你有一个带有“lkmi”的Category对象吗在url字段中?100%确定…如果我使用python manage.py shell并使用“category.objects.get(url='lkmi')”查找类别,它将返回所需的类别。根据视图的注释,它甚至没有进入视图函数,因为在控制台中,它不会输出“函数索引\页面”,即使它是函数的第一行。此外,如果我尝试使用blog.url中定义的URLconf提供的url“127.0.0.1:8000/lkmi,Django尝试了这些URL模式,顺序如下:1。管理员/-2。name='index_page']-3。post/[name='post_detallado']。但是如果我只输入“127.0.0.1:8000/随便什么”,错误就是没有与给定查询匹配的Categoria。因此,在本例中,它输入了函数,并给出了404错误,即没有找到匹配的类别。我仍然不明白为什么当我尝试进入lkmi url时它不执行视图..哦,对不起,那是因为我用西班牙语编程的,我把它改成了英语来提问,这只是堆栈溢出问题,不在我的代码中,但感谢您的注意。。。如果这是一个错误,那么每当我尝试加载任何页面时,它都将无法加载,而不仅仅是lkmi页面,因为每个类别都是通过相同的视图加载的(索引页面)
Using the URLconf defined in blog.urls, Django tried these URL patterns, in this order:
1. admin/ - 
2. <cat> name='index_page'] - 
3. post/<int:pk> [name='post_detallado']. 
No Categoria matches the given query.
def index_page(request, cat):
    categoria = get_object_or_404(Category,url=cat)
    postes = categoria.get_postes()
    return render(request, 'index.html',{'postes': postes,
                                         'categoria': categoria})