我在创建 Web 应用程序时遇到 python Django1.11.4 的反向 url 问题
I am getting problem in reverse url of python Django1.11.4 for creating the web application
django.core.exceptions.ImproperlyConfigured: 在 include() 中指定命名空间而不
不支持提供 app_name。在包含的模块中设置 app_name 属性,
或传递一个包含模式列表的二元组,并改为 app_name。
我尝试了多种不同的变体
请看我的代码:
我有一个urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
from django.urls import path
from .views import home_page, about_page, room_page, login_page, register_page
urlpatterns = [
url(r'^$', home_page),
url(r'^home/$', home_page),
url(r'^about/$', about_page),
url(r'^room/$', room_page),
url(r'^login/$', login_page),
url(r'^register/$', register_page),
url(r'^products/', include("products.urls", namespace='products')),
url(r'^youadmin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py-myapp
from django.conf.urls import url
from .views import (
ProductListView,
ProductDetailSlugView,
)
urlpatterns = [
url(r'^$', ProductListView.as_view(), name='list'),
url(r'^(?P<slug>[\w-]+)/$', ProductDetailSlugView.as_view(), name='detail'),
]
一个card.html
<div class="card" style="width: 18rem;">
{% if instance.image %}
<a href="{{ instance.get_absolute_url }}"><img src="{{ instance.image.url }}" class="card-img-top" alt="{{ instance.title }} logo"></a>
{% endif %}
<div class="card-body">
<h5 class="card-title">{{ instance.title }}</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="{{ instance.get_absolute_url }}" class="btn btn-primary">view</a>
<!-- <a href="{% url 'products:detail' slug=instance.slug %}" class="btn btn-warning">URL shortc</a> -->
</div>
</div>
一个models.py
import random
import os
from django.db import models
from django.db.models.signals import pre_save, post_save
from .utils import unique_slug_generator
from django.urls import reverse
def get_filename_ext(filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splittext(base_name)
return name, ext
def upload_image_path(instance, filename):
print(instance)
print(filename)
new_filename = random.randint(1, 3455645632)
name, ext = get_filename_ext(filename)
final_filename = '{new_filename}{ext}'.format(new_filename=new_filename, ext=ext)
return "Products/{new_filename}/{final_filename}",format(
new_filename=new_filename,
final_filename=final_filename
)
class Product(models.Model):
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True, blank=True)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=20, default=39.99)
image = models.ImageField(upload_to='products/', null=True, blank=True)
def get_absolute_url(self):
return "/products/{slug}/".format(slug=self.slug)
return reverse("products:detail", kwargs={"slug": self.slug})
def __str__(self):
return self.title
def __unicode__(self):
return self.title
def product_pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
pre_save.connect(product_pre_save_receiver, sender=Product)
您遇到的错误是不言自明的。
参考https://docs.djangoproject.com/en/1.11/ref/urls/#include
include(module, namespace=None, app_name=None)
include(pattern_list)
<b>include((pattern_list, app_namespace), namespace=None)</b>
include((pattern_list, app_namespace, instance_namespace))
根据错误消息,您应该依赖第三个选项
我假设你调用了你的应用程序 products
..
url(r'^products/', include(('products.urls', 'products'), namespace='products')),
..
django.core.exceptions.ImproperlyConfigured: 在 include() 中指定命名空间而不 不支持提供 app_name。在包含的模块中设置 app_name 属性, 或传递一个包含模式列表的二元组,并改为 app_name。
我尝试了多种不同的变体
请看我的代码:
我有一个urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
from django.urls import path
from .views import home_page, about_page, room_page, login_page, register_page
urlpatterns = [
url(r'^$', home_page),
url(r'^home/$', home_page),
url(r'^about/$', about_page),
url(r'^room/$', room_page),
url(r'^login/$', login_page),
url(r'^register/$', register_page),
url(r'^products/', include("products.urls", namespace='products')),
url(r'^youadmin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py-myapp
from django.conf.urls import url
from .views import (
ProductListView,
ProductDetailSlugView,
)
urlpatterns = [
url(r'^$', ProductListView.as_view(), name='list'),
url(r'^(?P<slug>[\w-]+)/$', ProductDetailSlugView.as_view(), name='detail'),
]
一个card.html
<div class="card" style="width: 18rem;">
{% if instance.image %}
<a href="{{ instance.get_absolute_url }}"><img src="{{ instance.image.url }}" class="card-img-top" alt="{{ instance.title }} logo"></a>
{% endif %}
<div class="card-body">
<h5 class="card-title">{{ instance.title }}</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="{{ instance.get_absolute_url }}" class="btn btn-primary">view</a>
<!-- <a href="{% url 'products:detail' slug=instance.slug %}" class="btn btn-warning">URL shortc</a> -->
</div>
</div>
一个models.py
import random
import os
from django.db import models
from django.db.models.signals import pre_save, post_save
from .utils import unique_slug_generator
from django.urls import reverse
def get_filename_ext(filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splittext(base_name)
return name, ext
def upload_image_path(instance, filename):
print(instance)
print(filename)
new_filename = random.randint(1, 3455645632)
name, ext = get_filename_ext(filename)
final_filename = '{new_filename}{ext}'.format(new_filename=new_filename, ext=ext)
return "Products/{new_filename}/{final_filename}",format(
new_filename=new_filename,
final_filename=final_filename
)
class Product(models.Model):
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True, blank=True)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=20, default=39.99)
image = models.ImageField(upload_to='products/', null=True, blank=True)
def get_absolute_url(self):
return "/products/{slug}/".format(slug=self.slug)
return reverse("products:detail", kwargs={"slug": self.slug})
def __str__(self):
return self.title
def __unicode__(self):
return self.title
def product_pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
pre_save.connect(product_pre_save_receiver, sender=Product)
您遇到的错误是不言自明的。
参考https://docs.djangoproject.com/en/1.11/ref/urls/#include
include(module, namespace=None, app_name=None)
include(pattern_list)
<b>include((pattern_list, app_namespace), namespace=None)</b>
include((pattern_list, app_namespace, instance_namespace))
根据错误消息,您应该依赖第三个选项
我假设你调用了你的应用程序 products
..
url(r'^products/', include(('products.urls', 'products'), namespace='products')),
..