简体   繁体   English

Django 导入错误:无法导入“名称”

[英]Django Import error: cannot import "name"

SOLVED解决了

I got an error i dont understand: cannot import name "UserUpdateForm" from "users.forms".我收到一个我不明白的错误:无法从“users.forms”导入名称“UserUpdateForm”。

Im doing the django tutorial from corey schafer, and we are creating a form as a class to update the users profiles of a blog page, then importing it to views.py and calling it in a function and when i try to run the server it pops up that error.我正在从 corey schafer 做 django 教程,我们正在创建一个表单作为一个类来更新博客页面的用户配置文件,然后将其导入到 views.py 并在函数中调用它,当我尝试运行服务器时弹出那个错误。 I already looked for other questions but in general they say that the problem is a circular import, but i cant figure if it is my case and where it is.我已经在寻找其他问题,但总的来说,他们说问题是循环导入,但我无法确定这是否是我的情况以及它在哪里。 Im relatively new to programming so i dont really understand how this works, any help will be aprecciated:我对编程比较陌生,所以我不太明白这是如何工作的,任何帮助都会受到赞赏:

(Already tried to import the UserUpdateForm inside my profile function and didnt work) (已经尝试在我的配置文件功能中导入 UserUpdateForm 并且没有用)

Forms.py表单.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]

class UserUptadeForm(forms.ModelForm):
    email = forms.EmailField()
    class Meta:
        model = User
        fields = ["username", "email"]

class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ["image"]

views.py视图.py

from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from django.contrib.auth.decorators import login_required



def register(request):
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get("username")
            messages.success(request, f"Your account has been created! You are now able to log")
            return redirect("login")
    else:
        form = UserRegisterForm()
    return render(request,"users/register.html",{"form": form})

@login_required
def profile(request):
    u_form = UserUpdateForm.forms()
    p_form = ProfileUpdateForm.forms()

    context= {
    "u_form": u_form,
    "p_form": p_form
    }
    return render(request, "users/profile.html")

forms.py的表单从UserUptadeForm重命名为UserUpdateForm (切换字母dt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM