我是一个学习 django 的初学者。在做我的项目时,我遇到了一个错误,现在是堆栈。 运行“python manage.py makemigrations”时,出现以下错误:
类型错误:“模块”对象不可迭代
我需要帮助才能尽快开始解决问题。
这是我的代码
myapp/views.py
from django.shortcuts import render, redirect
from .models import Book, UserBook
from django.contrib.auth.decorators import login_required
# Create your views here.
def home(request):
books = Book.objects.all()
return render(request, 'home.html', {'books': books})
def library(request):
user_books = UserBook.objects.filter(user=request.user)
return render(request, 'library.html', {'user_books': user_books})
def add_book(request, book_id):
book = Book.objects.get(id=book_id)
user_book = UserBook.objects.create(user=request.user, book=book)
return redirect('library')
myapp/urls.py
'''Defines the urls for the library application'''
from django.urls import path
from . import views
app_name = 'library'
urlpatterns = [
#homepage
path('', views.index, name='home'),
path('library/', views.library, name='library'),
path('add_book/<int:book_id>/', views.add_book, name='add_book'),
]
traceback
Traceback (most recent call last):
File "/Users/in/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/urls/resolvers.py", line 740, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/in/Desktop/ArenaStudy/manage.py", line 22, in <module>
main()
File "/Users/in/Desktop/ArenaStudy/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/in/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/Users/in/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/inn/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/inn/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/management/base.py", line 454, in execute
self.check()
File "/Users/inn/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/management/base.py", line 486, in check
all_issues = checks.run_checks(
File "/Users/inn/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/Users/inno/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/inno/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/checks/urls.py", line 24, in check_resolver
return check_method()
File "/Users/inno/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/urls/resolvers.py", line 520, in check
messages.extend(check_resolver(pattern))
File "/Users/inn/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/core/checks/urls.py", line 24, in check_resolver
return check_method()
File "/Users/inn/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/urls/resolvers.py", line 519, in check
for pattern in self.url_patterns:
File "/Users/inno/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/utils/functional.py", line 47, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/inno/.local/share/virtualenvs/ArenaStudy-g4tNO7nf/lib/python3.10/site-packages/django/urls/resolvers.py", line 748, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'library' from '/Users/inno/Desktop/ArenaStudy/library/__init__.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
myapp/models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
grade = models.CharField(max_length=30)
book_cover = models.ImageField(upload_to='images/')
published_date = models.DateField()
allowed_users = models.ManyToManyField(User, related_name='allowed_books')
@classmethod
def add_book(cls, title, author, published_date):
#Create a new book instance
new_book = cls(title=title, author=author, published_date=published_date,)
# Save the new book instance to the database
new_book.save()
return new_book
def delete_book(self):
'''delete the book instance from the database'''
self.delete()
class UserBook(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
current = models.BooleanField(default=False)
read = models.BooleanField(default=False)
我尝试在网上获取一些答案,但我没有得到任何明确的方法来解决问题
编写问题时,请将文件和输出分成不同的片段,以便更容易阅读和理解。
关于您的问题,您面临的错误是:
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'library' from '/Users/inno/Desktop/ArenaStudy/library/__init__.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
由于您有一个具有有效模式的
urlpatterns
变量,该错误可能是由于循环导入所致。
尝试改变
from . import views
类似的事情
from .views import index, library, add_book
看看是否可以解决问题。
希望这有帮助!