请为什么会出现循环导入错误

时间:2019-07-19 12:59:09

标签: python django django-views

我正在处理django项目,但是它返回包含的urlconf“ myapp.urls”似乎没有任何模式。

我尝试检查视图以确保正确导入所有内容

from django.contrib import admin
from django.urls import path
from .views import home
from accounts.views import login_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',home),
    path('accounts/login/', login_view),
]

我希望该网站能够运行并将我重定向到登录页面 这是我在与urls.py

相同目录中的视图
from django.shortcuts import render
from django.contrib.auth.decorators import login_required

@login_required
def home(request):
    return render(request,"home.html")

这是帐户的views.py

from django.shortcuts import render,redirect

from django.contrib.auth import(
    authenticate,
    get_user_model,
    login,
    logout
)

from .forms import UserLoginForm, UserRegisterForm

def login_view(request):
    next = request.GET.get('next')
    form = UserLoginForm()
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username,password=password)
        login(request,user)
        if next:
            return redirect(next)
        return redirect("/")

    context = {
        'form': form,
    }
    return render(request, "login.html",context)

1 个答案:

答案 0 :(得分:1)

当我在Django 2.2上运行您的项目时,看不到循环导入。相反,我看到了错误:

import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
import os

directory_in_str = 'building_data'
directory = os.fsencode(directory_in_str)

json_data = []
for file in os.listdir(directory):
    with open(directory_in_str+'/'+filename) as file:
        data = json.load(file)
    json_data.append( json_normalize(data['rooms']) )

df_all = pandas.DataFrame.from_records( json_data )

print(df_all)

查看您的表单,您没有设置字段,而是使用django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form UserRegisterForm needs updating.

model = Userfields = [...]

更改它,以设置class UserRegisterForm(forms.ModelForm): ... class Meta: model = Userfields = [ 'username', 'email', 'password', "c_password" ] 。您可以删除fields'password',因为您是在表单上分别定义它们的。

'c_password'