我创建了应用程序“login”
django-admin.py startapp login
在settings.py中,我添加了应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login',
]
我的views.py文件内容
#views.py
from login.forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
@csrf_protect
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
email=form.cleaned_data['email']
)
return HttpResponseRedirect('/register/success/')
else:
form = RegistrationForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response(
'registration/register.html',
variables,
)
def register_success(request):
return render_to_response(
'registration/success.html',
)
def logout_page(request):
logout(request)
return HttpResponseRedirect('/')
@login_required
def home(request):
return render_to_response(
'home.html',
{ 'user': request.user }
)
urls.py
from django.conf.urls import patterns, include, url
from login.views import *
urlpatterns = patterns('',
url(r'^logout/$', logout_page),
url(r'^register/$', register),
url(r'^register/success/$', register_success),
url(r'^home/$', home),
)
当我启动应用时,
(project) H:\Django\myapp>python manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x05CDD9C0>
Traceback (most recent call last):
File "C:\Users\kalu\Envs\project\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\kalu\Envs\project\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "C:\Users\kalu\Envs\project\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\kalu\Envs\project\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\kalu\Envs\project\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\kalu\Envs\project\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\kalu\Envs\project\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\kalu\Envs\project\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\kalu\Envs\project\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'login'
我试图按照一些django教程创建一个登录但我在启动服务器时不断收到此错误。有人可以帮忙解决这个问题吗?
答案 0 :(得分:0)
问题在于您的目录结构,在修复之后,您应该对403执行以下操作:
对于CSRF验证,您很可能忘记添加
{% csrf_token %}
到你的表格。
答案 1 :(得分:0)
我面临着同样的问题。检查您的树结构。 您的目录结构应与此类似-
C:login/
│ admin.py
│ apps.py
│ forms.py
│ models.py
│ tests.py
│ views.py
│ __init__.py
│
├───migrations
│ │ __init__.py
│ │
│ └───__pycache__
│ __init__.cpython-37.pyc
│
├───templates
│ └───login
│ index.html
│ login.html
│
└───__pycache__
admin.cpython-37.pyc
forms.cpython-37.pyc
models.cpython-37.pyc
views.cpython-37.pyc
__init__.cpython-37.pyc
摘要:检查您的登录文件夹,该文件夹应具有forms.py