【问题标题】:SyntaxError app_name while using Django使用 Django 时出现 SyntaxError app_name
【发布时间】:2018-07-08 12:22:41
【问题描述】:

大家好,我编辑了这篇文章,但仍然出现错误,请有人帮忙。

错误是:

  File "C:\Users\myshop\myshop\urls.py", line 25, in <module>
    url(r'^', include('shop.urls', namespace='shop')),
  File "C:\Users\Faruq\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include
    'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

urls.py 是这样的:

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

app_name='shop'

urlpatterns = [
    url('admin/', admin.site.urls),
    url(r'^', include('shop.urls', namespace='shop')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                                document_root=settings.MEDIA_ROOT)

【问题讨论】:

  • 您在上一行缺少一个右括号/括号。投票结束这个问题。
  • 我投票决定将此问题作为题外话结束,因为这是一个 OP 本可以解决的微不足道的问题。
  • 欢迎来到本站!查看tourhow-to-ask page 了解更多关于提出问题以吸引高质量答案的信息。您可以edit your question 包含更多信息。对拼写错误的近距离投票对于该网站来说是正常的——不要气馁。祝你好运!
  • 好吧,错误信息告诉你该怎么做:在包含的模块中提供一个应用程序名,而不是在这个模块中(你有一个名为 shop 的应用程序并将其添加到 INSTALLED_APPS对吧?)

标签: python django syntax-error


【解决方案1】:
url(r'^', include('shop.urls', namespace='shop'),

您在这一行缺少一个右括号。

第三方库不能在您的代码中导致SyntaxErrors。

编辑:您将括号放在错误的位置。参考the docs

url(path, view_or_include, namespace=string)

你需要有url(r'^', include('shop.urls'), namespace='shop'),

请不要在回答后将问题编辑为完全不同的问题。 :(

【讨论】:

  • 发生在我们所有人身上。
  • 更新@MadeinLondon
【解决方案2】:

从这里到:

url(r'^', include('shop.urls', namespace='shop'),

改成这样:

url(r'^', include('shop.urls', namespace='shop')),

【讨论】:

  • 感谢我更正了它,但仍然遇到语法问题。我编辑了帖子来解释。
猜你喜欢
  • 2018-10-26
  • 2012-09-27
  • 1970-01-01
  • 2019-04-28
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
相关资源
最近更新 更多