ImportError:无法导入名称' UserForm'

时间:2017-08-25 23:06:04

标签: python django

我使用了几乎相同的代码,我已经在另一个项目中工作,所以我无法确定问题,然后它看起来像是来自我的网址中的某些东西,因为它属于观点?除此之外,我不确定为什么我会收到此错误。请帮忙。

错误追溯:

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'))

app 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'),
]

project 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')),
]

如果您需要查看其他内容,请与我们联系。谢谢。

3 个答案:

答案 0 :(得分:2)

您的forms.py中有拼写错误。您有UserFrom而不是UserForm

答案 1 :(得分:0)

你声明你认为它来自你的urls.py,但是

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

因此错误来自view.py line2 ....

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

仔细检查导入...如有必要,使用django交互式shell(运行每一行以查看它是否有效)

答案 2 :(得分:0)

更改第 2 行您的 views.py 文件来自

    from accounts.forms import UserForm
                To
    from <appname> import Form