繁体   English   中英

django urlconf直接从根到包含的应用程序逻辑

[英]django urlconf direct from root to included app logic

我已经尝试解决这个问题了一段时间,但一直没有找到有效的解决方案。 也许有人可以帮助指出更好的方法!

我正在使用根urlconf将两个请求重定向到包含的应用程序:

url("^about/news/", include("kdi.urls")),
url("^about/recognition/", include("kdi.urls")),

这是kdi应用的urlconf:

url(r"^$", "kdi.views.news", name="news"),

# this is the pattern that needs to change:
url(r"^$", "kdi.views.recog", name="recog"),

使用从根^about/news/^about/recognition到应用程序urlconf中的^$的更精细的重定向似乎更聪明。 这仅适用于一种模式,但我想对其进行扩展以使其适用于两种模式。

^about/从根目录引导到可以在kdi应用中检查^/news$^/recognition$的应用中, kdi会更聪明? 如果未进行匹配,那还能使用根的全包^吗? 是否可以从urlconf中检查request.path ,然后使用if语句将其定向到正确的视图? 还是可以在根目录中使用name字段,然后通过应用程序的网址格式中的该名称进行访问?

只是有点麻烦,以此来确定逻辑!

编辑:

从根urlconf中删除了namespace字段以限制混乱

您在这里做的事情很奇怪,我不知道为什么。

您只应添加一次应用网址。 但是该包含文件中的每个URL都必须不同 -否则Django无法知道如何路由请求。

因此,主要的urls.py应该只是:

url("^about/", include("kdi.urls")),

和应用程序应该是:

url(r"^news/$", "kdi.views.news", name="news"),
url(r"^recognition/$", "kdi.views.recog", name="recog")

Python Django - 包含的 URLconf ' <module '{spp}.urls' from {path} does not appear to have any patterns in it< div><div id="text_translate"><p> 我正在阅读 Eric Matthes 撰写的 Python Crash Course 中的第 18 章,这是一个关于如何使用 Django 创建 web 应用程序的教程。 目前我正在映射 URL,编写视图,编写模板,然后尝试在我的系统上部署应用程序,以便我可以看到我创建的主页。</p><p> 以下是相关文件/代码:</p><p> urls.py-learning_log</p><pre> from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('learning_logs.urls')) ]</pre><p> urls.py-learning_logs</p><pre> """Defines URL patterns for learning_logs.""" from django.urls import path from. import views app_name = 'learning_logs' url_patterns = [ # Home page path('', views.index, name='index'), ]</pre><p> 视图.py</p><pre> from django.shortcuts import render def index(request): """The home page for Learning Log.""" return render(request, 'learning_logs/index.html')</pre><p> 索引.html</p><pre> &lt;p&gt;Learning Log&lt;/p&gt; &lt;p&gt;Learning Log helps you to keep track of your learning, for any topic you're learning about.&lt;/p&gt;</pre><p> 当我尝试使用 Powershell 为 Windows 的项目的虚拟环境中使用runserver命令创建服务器时,这是我得到的错误:</p><pre> (ll_env) PS C:\Users\Samie\Desktop\python_work\learning_log&gt; python manage.py runserver Watching for file changes with StatReloader Performing system checks... Exception in thread django-main-thread: Traceback (most recent call last): File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 590, in url_patterns iter(patterns) TypeError: 'module' object is not iterable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Samie\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Users\Samie\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\utils\autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\management\commands\runserver.py", line 117, in inner_run self.check(display_num_errors=True) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\management\base.py", line 392, in check all_issues = self._run_checks( File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\management\base.py", line 382, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\registry.py", line 72, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\urls.py", line 13, in check_url_config return check_resolver(resolver) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 408, in check messages.extend(check_resolver(pattern)) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 407, in check for pattern in self.url_patterns: File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\utils\functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 597, in url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) django.core.exceptions.ImproperlyConfigured: The included URLconf '&lt;module 'learning_logs.urls' from 'C:\\Users\\Samie\\Desktop\\python_work\\learning_log\\learning_logs\\urls.py'&gt;' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.</pre><p> 作为一个超级绿色的 Python/Django 用户,这对我来说已经消化了很多。 任何人都可以在这里看到我哪里出错了吗? 我知道我为这个项目安装的 Django 版本比本书出版时(2019 年)新,这可能是问题吗? 感谢您提供的任何帮助!</p></div></module>

[英]Python Django - the included URLconf '<module '{spp}.urls' from {path} does not appear to have any patterns in it

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:在中间件中用request.urlconf覆盖ROOT_URLCONF Python Django - 包含的 URLconf ' <module '{spp}.urls' from {path} does not appear to have any patterns in it< div><div id="text_translate"><p> 我正在阅读 Eric Matthes 撰写的 Python Crash Course 中的第 18 章,这是一个关于如何使用 Django 创建 web 应用程序的教程。 目前我正在映射 URL,编写视图,编写模板,然后尝试在我的系统上部署应用程序,以便我可以看到我创建的主页。</p><p> 以下是相关文件/代码:</p><p> urls.py-learning_log</p><pre> from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('learning_logs.urls')) ]</pre><p> urls.py-learning_logs</p><pre> """Defines URL patterns for learning_logs.""" from django.urls import path from. import views app_name = 'learning_logs' url_patterns = [ # Home page path('', views.index, name='index'), ]</pre><p> 视图.py</p><pre> from django.shortcuts import render def index(request): """The home page for Learning Log.""" return render(request, 'learning_logs/index.html')</pre><p> 索引.html</p><pre> &lt;p&gt;Learning Log&lt;/p&gt; &lt;p&gt;Learning Log helps you to keep track of your learning, for any topic you're learning about.&lt;/p&gt;</pre><p> 当我尝试使用 Powershell 为 Windows 的项目的虚拟环境中使用runserver命令创建服务器时,这是我得到的错误:</p><pre> (ll_env) PS C:\Users\Samie\Desktop\python_work\learning_log&gt; python manage.py runserver Watching for file changes with StatReloader Performing system checks... Exception in thread django-main-thread: Traceback (most recent call last): File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 590, in url_patterns iter(patterns) TypeError: 'module' object is not iterable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Samie\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Users\Samie\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\utils\autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\management\commands\runserver.py", line 117, in inner_run self.check(display_num_errors=True) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\management\base.py", line 392, in check all_issues = self._run_checks( File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\management\base.py", line 382, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\registry.py", line 72, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\urls.py", line 13, in check_url_config return check_resolver(resolver) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 408, in check messages.extend(check_resolver(pattern)) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 407, in check for pattern in self.url_patterns: File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\utils\functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Samie\Desktop\python_work\learning_log\ll_env\lib\site- packages\django\urls\resolvers.py", line 597, in url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) django.core.exceptions.ImproperlyConfigured: The included URLconf '&lt;module 'learning_logs.urls' from 'C:\\Users\\Samie\\Desktop\\python_work\\learning_log\\learning_logs\\urls.py'&gt;' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.</pre><p> 作为一个超级绿色的 Python/Django 用户,这对我来说已经消化了很多。 任何人都可以在这里看到我哪里出错了吗? 我知道我为这个项目安装的 Django 版本比本书出版时(2019 年)新,这可能是问题吗? 感谢您提供的任何帮助!</p></div></module> 改变Django的ROOT_URLCONF 所包含的urlconf中没有任何模式+ Django Django 说配置不当:包含的 URLconf 中似乎没有任何模式 包含的 URLconf 中似乎没有任何模式。 错误Django Django错误“包含的URLconf中似乎没有任何模式” Django 1.5随附的urlconf中没有任何模式 Django - 包含的 urlconf 中没有任何模式 django.core.exceptions.ImproperlyConfigured:包含的URLconf&#39;&#39;中似乎没有任何模式
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM