2017-09-27 195 views
1

下面的代码给出了这样的错误导入错误:没有名为“模型”

导入错误模块:没有名为模块“模型”

我不知道为什么。我想在django中自定义我自己的注册表单。

的错误是在一行:

from models import CustomUser in forms.py 

forms.py:

from django.contrib.auth.forms import UserCreationForm, UserChangeForm 

from models import CustomUser 

class CustomUserCreationForm(UserCreationForm): 

def __init__(self, *args, **kargs): 
    super(CustomUserCreationForm, self).__init__(*args, **kargs) 
    del self.fields['username'] 

class Meta: 
    model = CustomUser 
    fields = ["email",] 

class CustomUserChangeForm(UserChangeForm): 

def __init__(self, *args, **kargs): 
    super(CustomUserChangeForm, self).__init__(*args, **kargs) 
    del self.fields['username'] 

class Meta: 
    model = CustomUser 

register.html:

{% extends "account/base.html" %} 
{% load crispy_forms_tags %} 
{% load i18n %} 

{% block head_title %}{% trans "Signup" %}{% endblock %} 

{% block content %} 
<div class= 'col-md-4 col-md-offset-4'> 
<h1>{% trans "Register" %}</h1> 

<p>{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p> 

<form acction="account/register/" method="post"> 
{% csrf_token %} 
{{ form|crispy }} 


<input type="submit" value="Register" /> 
<button type="submit" class= 'btn btn-default'>{% trans "Sign Up" %} &raquo;</button> 
</form> 
</div> 
{% endblock %} 

views.py:

from django.contrib.auth.decorators import login_required 
from django.shortcuts import render, redirect 
from custom_user.forms import CustomUserCreationForm 

#Create your views here 
def home(request): 
    return render(request, "home.html") 

def login(request): 
    c = {} 
    c.update(csrf(request)) 
    return render(request, "login.html", c) 

def auth_view(request): 
    username = request.POST.get['username', ''] 
    password = request.POST.get['password', ''] 
    user = auth.authenticate(username=username, password=password) 

    if user is not None: 
     auth.login(request, user) 
     return HTTpResponseRedirect('account/login') 
    else: 
     return HTTpResponseRedirect('account/login') 


def logout(request): 
    auth.logout(request) 
    return render(request, 'logout.html') 
+0

在哪里你的models.py? – blueCat

回答

7

您需要添加点

from .models import CustomUser 
# ^^^ 

Ø最好的方式使用APP_NAME

from custom_user.models import CustomUser 

,并为你的第二个错误,你可以简单的添加空排除到元:

class CustomUserChangeForm(UserChangeForm): 

def __init__(self, *args, **kargs): 
    super(CustomUserChangeForm, self).__init__(*args, **kargs) 
    del self.fields['username'] 

class Meta: 
    model = CustomUser 
    exclude =() # THIS ROW 
+0

我试着看起来像这样:django.core.exceptions.ImproperlyConfigured:禁止创建没有'fields'属性或'exclude'属性的ModelForm; CustomUserChangeForm表单需要更新。你能解释为什么我仍然有一个错误? – nuradilla

+0

更新了答案,但不好主意尝试修复所有错误的意见 –

+0

感谢您的帮助。我确实很感激:) – nuradilla

相关问题