姜戈。ImportError:尝试相对导入超出顶级包

Dmi*_*try 4 python django structure path project

我是新手。我刚刚开始写一个 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'),
]
Run Code Online (Sandbox Code Playgroud)

在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'})
Run Code Online (Sandbox Code Playgroud)

但是当尝试从第二个views.py导入函数时

from django.shortcuts import render


def list_notes(request):
    return render(request, 'iRay_working_with_notes/list_notes.html')
Run Code Online (Sandbox Code Playgroud)

我收到错误:

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

我找到了很多关于为什么会出现这个错误的理论信息。但我仍然无法弄清楚是否有一种简单的方法可以进行相对或绝对导入,或者我只是没有正确构建我的项目?

Raz*_*ein 8

  1. 导入错误
from ..iRay_working_with_notes.views import list_notes
Run Code Online (Sandbox Code Playgroud)

应该

from iRay_working_with_notes.views import list_notes
Run Code Online (Sandbox Code Playgroud)
  1. 重定向需要来自 urls-pattern 的视图名称:
redirect('name-of-my-view-pattern')
Run Code Online (Sandbox Code Playgroud)

因此,请为 list_notes 视图创建一个 url-pattern 条目,并将模式名称作为重定向的参数。

为什么?因为重定向告诉客户端的浏览器加载目标重定向页面,所以它需要一个 url(由 django 通过 url-pattern 生成),因为目标页面将从浏览器中调用。

当然,从技术上讲,您可以导入视图并直接从另一个模块调用它,因为它只是一个 python 函数 - 但这不是 http 重定向(并且不会将浏览器中的 url 更改为重定向页面),此外,这也不是这确实是 django 请求/响应架构的想法。

  • 谢谢回复。PyCharm 在行中显示错误: from iRay_working_with_notes.views import list_notes 但它有效 (2认同)
  • 在 PyCharm -> 右键单击​​ iRayProject 并标记为源文件夹...然后 Pycharm 错误将消失 (2认同)