Django. ImportError: 尝试超出顶级包的相对导入

4
我是一位新手。我刚开始写一个 Django 项目。它被称为 iRayProject,由两个应用程序 iRay_user_authentication 和 iRay_working_with_notes 组成: 此处查看项目结构 iRay_user_authentication - 这是一个标准的 Django 应用程序,用于注册。
以下是它的 urls.py:
from django.urls import path
from .views import login_user, registration

urlpatterns = [
    path('', login_user, name='login_user'),
    path('registration', registration, name='registration'),
]

在views.py中,我想使用重定向将注册用户发送到项目的第二个应用程序。
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from django.db import IntegrityError
from django.contrib.auth import login, logout, authenticate
from ..iRay_working_with_notes.views import list_notes


def login_user(request):
    if request.method == 'GET':
        return render(request, 'iRay_user_authentication/login.html', {'form': AuthenticationForm})


def registration(request):
    if request.method == 'GET':
        return render(request, 'iRay_user_authentication/registration.html', {'form': UserCreationForm})
    else:
        if '_guest' in request.POST:
            pass
        else:
            if request.POST['password1'] == request.POST['password2']:
                try:
                    user = User.objects.create_user(request.POST['username'], password=request.POST['password1'])
                    user.save()
                    login(request, user)
                    return redirect(list_notes)
                except IntegrityError:
                    return render(request, 'iRay_user_authentication/registration.html', {'form': UserCreationForm,
                                                                                          'error': 'name is busy '
                                                                                          })
            else:
                return render(request, 'todo_app/registration.html', {'form': UserCreationForm,
                                                                      'error': 'passwords not math'})

但是当尝试从第二个views.py文件中导入函数时,会出现问题。
from django.shortcuts import render


def list_notes(request):
    return render(request, 'iRay_working_with_notes/list_notes.html')

我遇到了一个错误:

ImportError: 尝试相对于顶级包之外的位置进行导入

我发现有很多理论信息可以解释这个错误的原因。 但我仍然无法确定是否有一种简单的方法来实现相对或绝对导入,或者是我的项目结构不正确?


你应该将正在执行的模块放置在项目的根文件夹中。该文件夹会被添加到 PYTHONPATH 中,并成为导入基础。 - Klaus D.
重定向不使用视图函数,而是使用URL配置名称以及可选参数。 - dgw
1个回答

7
  1. 导入有误
from ..iRay_working_with_notes.views import list_notes

应该是

from iRay_working_with_notes.views import list_notes

重定向需要从urls-pattern获取视图名称:
redirect('name-of-my-view-pattern')

请为list_notes视图创建一个url-pattern条目,并将该模式名称作为重定向的参数。因为重定向是告诉客户端浏览器加载目标重定向页面,所以需要一个url(由Django通过url-pattern生成),因为目标页面将从浏览器中调用。
技术上当然可以直接从另一个模块导入并调用视图作为Python函数,但那不是HTTP重定向(并且不会更改浏览器中的URL到重定向的页面),而且这也不是Django请求/响应架构的真正想法。

2
感谢您的回复。 PyCharm在这一行中显示了一个错误:from iRay_working_with_notes.views import list_notes,但它确实可以工作。 - Dmitry
2
在 PyCharm 中 -> 右键单击 iRayProject 并标记为源文件夹...然后 Pycharm 错误将消失 - Razenstein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接