我创建了一个Django网站,一切正常,直到在urls.py中添加新路径的那一刻:
应用的urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.members_list, name='members_list_url'),
path('create/', views.MemberCreate.as_view(), name='member_create_url'),
path('<str:nickname>/', views.MemberPage.as_view(), name='member_page_url')
]
并出现错误消息:
(venv) D:\Documents\Desktop\percents_system\app\percentsystem>manage.py runserve
r
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\urls\
resolvers.py", line 581, 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:\program files\python36\Lib\threading.py", line 916, in _bootstrap_inn
er
self.run()
File "c:\program files\python36\Lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\utils
\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\core\
management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\core\
management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\core\
management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\core\
checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\core\
checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\core\
checks\urls.py", line 23, in check_resolver
return check_method()
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\urls\
resolvers.py", line 398, in check
for pattern in self.url_patterns:
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\utils
\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Documents\Desktop\percents_system\venv\lib\site-packages\django\urls\
resolvers.py", line 588, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'percentsystem.urls' 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.
项目的urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from .views import to_home
urlpatterns = [
path('admin/', admin.site.urls),
path('system/', include('system.urls')),
path('', to_home, name='home'),
]
我尝试将path('system/', include('system.urls'))
更改为re_path('system/', include('system.urls')),
它没有帮助。
另外,如果要注释path('system/', include('system.urls')),
字符串
from django.contrib import admin
from django.urls import path, include, re_path
from .views import to_home
urlpatterns = [
path('admin/', admin.site.urls),
#path('system/', include('system.urls')),
path('', to_home, name='home'),
]
然后服务器正常启动。 我不知道该怎么办,我已经尝试了2天来解决此问题。
此外,如果需要,还可以显示应用的views.py
from django.shortcuts import render
from django.shortcuts import redirect
from django.urls import reverse
from django.shortcuts import get_object_or_404
from django.views.generic import View
from .models import Member
from .utils import ObjectCreateMixin
from .forms import MemberForm
from django.core.paginator import Paginator
from django.contrib.auth.mixins import LoginRequiredMixin
def index(request):
return render(request, 'system/base_system.html')
def members_list(request):
search_query = request.GET.get('search', '')
if search_query:
members = Member.objects.filter(Q(name__icontains=search_query) | Q(surname__icontains=search_query))
paginator = Paginator(members, 2)
page_number = request.GET.get('page', 1)
page = paginator.get_page(page_number)
is_paginated = page.has_other_pages()
is_searching = True
href = "?search={}&page=".format(search_query, page_number)
print(is_searching)
if page.has_previous():
prev_url = '?search={}&page={}'.format(search_query, page.previous_page_number())
else:
prev_url = '?search={}'.format(search_query)
if page.has_next():
next_url = '?search={}&page={}'.format(search_query, page.next_page_number())
else:
next_url='?search={}'.format(search_query)
else:
members = Member.objects.all()
paginator = Paginator(members, 2)
page_number = request.GET.get('page', 1)
page = paginator.get_page(page_number)
is_paginated = page.has_other_pages()
is_searching = False
href = "?search={}&page=".format(search_query, page_number)
print(is_searching)
if page.has_previous():
prev_url = '?page={}'.format(page.previous_page_number())
else:
prev_url = ''
if page.has_next():
next_url = '?&page={}'.format(search_query, page.next_page_number())
else:
next_url=''
context = {
'page': page,
'is_paginated': is_paginated,
'prev_url': prev_url,
'next_url': next_url,
'search_query': search_query,
'href': href,
'members_amount': len(members)
}
return render(request, 'system/members_list.html', context=context)
class MemberCreate(ObjectCreateMixin, View):
model_form = MemberForm
template = 'system/member_create.html'
model = Member
class MemberPage(View):
def get(self, request, nickname):
member = get_object_or_404(Member, nickname__iexact = nickname)
return render(request, 'system/member_page.html', context={'member': member})
和应用程序的模型。py
from django.db import models
import os
from django.shortcuts import reverse
class Member(models.Model):
name = models.CharField(max_length=32, db_index=True)
surname = models.CharField(max_length=32, db_index = True)
position = models.CharField(max_length = 32, db_index=True)
email = models.EmailField(max_length=64, db_index=True)
stake = models.IntegerField(null=True)
nickname = models.CharField(max_length=32, unique=True)
reg_date = models.DateTimeField(auto_now_add=True)
hash = models.CharField(max_length=64, db_index=True)
tasks = models.ManyToManyField('Task', related_name='tasks')
stake_chart = models.ManyToManyField('StakeUpdate', related_name='stake_updates')
contact_info = models.TextField(db_index=True)
about = models.TextField(db_index=True)
card_mini = models.FilePathField()
card_full = models.FilePathField()
member_photo = models.FileField(upload_to = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media'))
member_photo_path = models.FilePathField(default=os.path.join(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media'), 'empty_user_photo.png'))
stake_chart_photo = models.FilePathField()
def get_absolute_url(self):
return reverse('member_page_url', kwargs={'nickname': self.nickname})
class Meta:
ordering = ['reg_date']
class Task(models.Model):
adresser = models.ManyToManyField('Member', related_name='member')
title = models.CharField(max_length=32, db_index=True, blank=True)
body = models.CharField(max_length = 512, db_index=True)
pub_date = models.DateTimeField(auto_now_add=True)
class StakeUpdate(models.Model):
new_stake = models.IntegerField(db_index=True)
change = models.IntegerField(db_index=True)
time = models.DateTimeField(auto_now_add=True)
comments = models.TextField(db_index=True, null=True)