首页 > 解决方案 > Python Django 3.1 包含的 URLconf 'mysite.urls' 似乎没有任何模式

问题描述

我目前正在通过教程学习 django。所以我在视图中将返回渲染更改为类通用。在 URL 中我添加了 as_view() 然后它给了我一个错误。

我的回溯:

  File "D:\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "D:\Anaconda3\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "D:\Anaconda3\lib\site-packages\django\core\management\base.py", line 396, in check
    databases=databases,
  File "D:\Anaconda3\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "D:\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "D:\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "D:\Anaconda3\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'mysite.urls' does not appear to have any patterns in it. If you see valid patter
ns in the file then the issue is probably caused by a circular import.

网址.py:

from django.urls import path
from . import views
app_name = 'myapp'

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:id>/', views.DetailView.as_view(), name='detail'),
    path('<int:id>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

视图.py:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from .models import Question
from django.template import loader
from django.views import generic

class IndexView(generic.listView):
   template_name = 'myapp/index.html'
   context_object_name = 'latest_question_list'
   def get_queryset(self):
       return Question.objects.order_by('-pub_date')


class DetailView(generic.DetailView):
   model = Question
   template_name = 'myapp/detail.html'


class ResultsView(generic.DetailView):
   model = Question
   template_name = 'myapp/results.html'


def vote(request, question_id):
   question = get_object_or_404(Question, id=question_id)
   try:
       selected_choice = question.choice_set.get(id=request.POST['choice'])
   except (KeyError, Choice.DoesNotExist):
       return render(request, 'myapp/detail.html',
                     {'question': question,
                      'error_message': "You didn't select a choice."}
                     )
   else:
       selected_choice.votes += 1
       selected_choice.save()
       return HttpResponseResponse(reverse_lazy('myapp:results', args=(question_id,)))

我考虑了其他教程和线程,但对我的主题一无所获。我想我错过了一些关于 ID 和 PK 的内容。

标签: python-3.xdjangodjango-views

解决方案


我收到此错误消息只是因为代码中的错误。导入模型页面时,我没有指定类名。models import classname 是在该页面中使用此类的正确位置。模型导入错误


推荐阅读