2017-08-25 97 views
0

我正在使用與我已經在不同項目中工作的代碼非常相似,因此我無法真正指出問題,但它看起來是來自某個我的網址與視圖有關嗎?除此之外,我不確定爲什麼會出現此錯誤。請幫忙。ImportError:無法導入名稱'UserForm'

錯誤回溯:

Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line 
    utility.execute() 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute 
    self.check() 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check 
    include_deployment_checks=include_deployment_checks, 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks 
    issues.extend(super(Command, self)._run_checks(**kwargs)) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks_run_checks 
    return checks.run_checks(**kwargs) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config 
    return check_resolver(resolver) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver 
    return check_method() 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check 
    for pattern in self.url_patterns: 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/anaconda/envs/test/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "/anaconda/envs/test/lib/python3.6/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 978, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 961, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 655, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 678, in exec_module 
    File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed 
    File "/Users/garrettlove/Desktop/colors/colors/urls.py", line 19, in <module> 
    from accounts import views 
    File "/Users/garrettlove/Desktop/colors/accounts/views.py", line 2, in <module> 
    from accounts.forms import UserForm 
ImportError: cannot import name 'UserForm' 

Form.py:

from django import forms 
from django.contrib.auth.models import User 
from accounts.models import User 

class UserFrom(forms.ModelForm): 
    password = forms.CharField(widget=forms.PasswordInput()) 

    class Meta(): 
     model = User 
     fields = ('username','email','password') 

Views.py:

from django.shortcuts import render 
from accounts.forms import UserForm 

from django.contrib.auth import authenticate,login,logout 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from django.contrib.auth.decorators import login_required 

# Create your views here. 
def user_login(request): 
    if request.method == 'POST': 
     username = request.POST.get('username') 
     password = request.POST.get('password') 

     user = authenticate(username=username,password=password) 

     if user: 
      if user.is_active: 
       login(request.user) 
       return HttpResponseRedirect(reverse('index')) 
      else: 
       return HttpResponse("Account now active") 

     else: 
      print("Login Unsuccessful") 
      return HttpResponse("Your username and/or password are not correct") 

    else: 
     return render(request,'accounts/login.html',{}) 

def register(request): 
    registered = False 

    if request.method == 'POST': 
     user_form = UserForm(data=request.POST) 

     if user_form.is_valid(): 
      user = user_form.save() 
      user.set_password(user.password) 

      registered = True 
     else: 
      print(user_form.errors) 

    else: 
     user_form = UserForm() 

    return render(request,'accounts/register.html',{'user_form':user_form,'registered':registered}) 

@login_required 
def user_logout(request): 
    logout(request) 
    return HttpResponseRedirect(reverse('index')) 

應用urls.py:

from django.conf.urls import url 
from accounts import views 

app_name = 'accounts' 

urlpatterns = [ 
    url(r'^register/$',views.register,name='register'), 
    url(r'^login/$',views.user_login,name='user_login'), 
    url(r'^logout/',views.user_logout,name='user_logout'), 
] 

項目urls.py:

from django.conf.urls import url 
from django.contrib import admin 
from django.conf.urls import include 
from accounts import views 
#from colorsets import views 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
# url(r'^$',views.index,name='index'), 
    url(r'^accounts/',include('accounts.urls')), 
] 

讓我知道如果你需要看別的。謝謝。

回答

2

您在forms.py中有錯字。您有UserFrom而不是UserForm

+0

這很簡單,我誦讀困難,所以我真的很感謝幫助,即使它很基本。 – Garrett

0

,你認爲它從你的urls.py來幽州,但

您的跟蹤表明 File "/Users/garrettlove/Desktop/colors/accounts/views.py", line 2, in <module> from accounts.forms import UserForm

這樣的錯誤是從2號線view.py未來....

1) from django.shortcuts import render 
2) from accounts.forms import UserForm 
3) 
4) from django.contrib.auth import authenticate,login,logout 

仔細檢查您的導入...如果需要,使用django交互式shell(運行每一行以查看它是否有效)

相關問題