django - NoReverseMatch 在 /products/ ,“产品”不是注册的命名空间
问题描述
我正在 Django 中制作 2 个应用程序。第一个称为“产品”,第二个称为“博客”。我做了第一个,一切正常,但是当我添加第二个时,两者都不起作用
网址.py
path('products/', include('products.urls')),
path('blog/', include('blog.urls')),
博客\urls.py
app_name = 'articles'
urlpatterns = [
path('<int:id>/', article_detail_view, name='article_detail'), ]
产品\urls.py
app_name = 'products'
urlpatterns = [
path('<int:id>/', product_detail_view, name='product_detail'), ]
有关代码的更多详细信息: src> blog : { templates > items > article_create.html 和 article_detail.html 和 article_list.html , forms , models , urls , views } , products { templates > products > product_create.html 和 product_detail.html 和product_list.html , 表单 , 模型 , url , 视图 }
article_create.html 和 product_create.html 包含相同的代码:
{% extends 'base.html' %}
{% block content %}
<form method="post"> {% csrf_token %}
{{ form.as_p }}
<input type="submit" , value="Save">
</form>
{% endblock %}
博客\模型:
from django.db import models
from django.urls import reverse
class Article(models.Model) :
title = models.CharField(max_length=130)
content = models.TextField(blank=True, null=True)
active= models.BooleanField(default=True)
def get_absolute_url(self):
return reverse("articles : article_detail",kwargs={"id":self.id})
博客\网址:
from django.contrib import admin
from django.urls import path
from blog.views import (
article_detail_view,
article_create_view,
article_list_view,
)
app_name = 'blog'
urlpatterns = [
path('<int:id>/', article_detail_view, name='article_detail'),
path('create/', article_create_view, name='article_create'),
path('', article_list_view, name='article_list'),
]
博客\视图:
from django.shortcuts import render , get_object_or_404 , redirect
from django.views.generic import CreateView , DeleteView , DetailView , ListView ,
UpdateView
from .models import Article
from .forms import ArticleForm
def article_detail_view(request,id):
obj=get_object_or_404(Article,id=id)
context = {
'object' : obj
}
return render( request , "articles/article_detail.html" , context )
def article_create_view(request):
form=ArticleForm(request.POST or None)
if form.is_valid():
form.save()
form = ArticleForm()
context = {
'form' : form
}
return render( request , "articles/article_create.html" , context )
def article_list_view(request):
queryset = Article.objects.all() #list of objects
context = {
'object_list' : queryset
}
return render( request , "articles/article_list.html" , context )
INSTALLED_APPS 包含“产品”和“博客”
我为产品文件(products\urls 和 product\views...)添加了相同的代码
我希望我现在就对你说清楚。
解决方案
原因是,对于应用产品,您的命名空间默认为“产品”,但对于您使用的应用博客,您将应用指定为“文章”
app_name 应该是您的应用程序的实际名称
app_name = 'blog'
urlpatterns = [
path('<int:id>/', article_detail_view, name='article_detail'), ]
推荐阅读
- git - Azure Git 存储库、Visual Studio、共享库和子模块
- mysql - MySQL 5.7:指定的键太长;最大密钥长度为 1000 字节
- ios - AVPlayer - 检索所有格式的音轨
- web-services - TYPO3 Webservice/Rest API 作为 Extbase 扩展的存储库
- linux - 从私人不安全注册表中提取图像时出现问题
- c# - Xamarin iOS 中的可操作推送通知
- javascript - Vue如何使用当前路由名称为主体设置数据属性
- mysql - 如何从主机连接 MySQL Docker 映像?
- c++ - 空 std::vector
> 函数返回后 - google-drive-api - 使用矩形锚创建评论