我一直在谷歌搜索并尝试这一点而且无法获得登录表单。
我一直关注这个tutorial,我已经重建了几次,希望能抓住一个我错过的小错误。我也一直在谷歌搜索错误和潜在的导致无济于事。
我的问题是: 当我运行我的Django应用并尝试访问http://localhost:9000/login/时,我遇到两个错误,当我点击刷新时来回切换在浏览器中。
错误1
TemplateDoesNotExist at /login/
login.html
Request Method: GET
Request URL: http://localhost:9000/login/
Django Version: 1.11.4
Exception Type: TemplateDoesNotExist
Exception Value:
login.html
Exception Location: /home/vagrant/gst/venv/lib/python2.7/site-packages/django/template/loader.py in select_template, line 53
Python Executable: /home/vagrant/gst/venv/bin/python2.7
Python Version: 2.7.4
Python Path:
['/home/vagrant/gst/tools',
'/home/vagrant/gst/venv/bin',
'/home/vagrant/gst/tools/tools',
'/home/vagrant/gst/venv/lib/python27.zip',
'/home/vagrant/gst/venv/lib/python2.7',
'/home/vagrant/gst/venv/lib/python2.7/plat-linux2',
'/home/vagrant/gst/venv/lib/python2.7/lib-tk',
'/home/vagrant/gst/venv/lib/python2.7/lib-old',
'/home/vagrant/gst/venv/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/home/vagrant/gst/venv/lib/python2.7/site-packages']
Server time: Sat, 17 Mar 2018 19:31:48 -0700
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /home/vagrant/gst/tools/tools/templates/login.html (Source does not exist)
错误2
NoReverseMatch at /login/
Reverse for '' not found. '' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://localhost:9000/login/
Django Version: 1.11.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for '' not found. '' is not a valid view function or pattern name.
Exception Location: /home/vagrant/gst/venv/lib/python2.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
Python Executable: /home/vagrant/gst/venv/bin/python2.7
Python Version: 2.7.4
Python Path:
['/home/vagrant/gst/tools',
'/home/vagrant/gst/venv/bin',
'/home/vagrant/gst/tools/tools',
'/home/vagrant/gst/venv/lib/python27.zip',
'/home/vagrant/gst/venv/lib/python2.7',
'/home/vagrant/gst/venv/lib/python2.7/plat-linux2',
'/home/vagrant/gst/venv/lib/python2.7/lib-tk',
'/home/vagrant/gst/venv/lib/python2.7/lib-old',
'/home/vagrant/gst/venv/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/home/vagrant/gst/venv/lib/python2.7/site-packages']
Server time: Sat, 17 Mar 2018 19:54:07 -0700
Error during template rendering
In template /home/vagrant/gst/tools/templates/base.html, error at line 17
Reverse for '' not found. '' is not a valid view function or pattern name.
17 <span class="sr-only">Toggle navigation</span>
我不知道该怎么做。对于 ERROR 1 ,该文件确实存在。我还注意到,当我点击浏览器刷新按钮时,每隔几次点击登录模板的路径就会来回切换:
/home/vagrant/gst/tools/tools/templates/login.html
和
/home/vagrant/gst/tools/templates/login.html
当加载了正确的路径时,我得到 ERROR 2 ,这也让我感到困惑,我不相信我有第17行的问题作为错误状态。我已经确认css静态文件中存在类标记。我试图将所有文件设置为完全权限777,因此我排除了权限问题。
非常感谢帮助。我的配置如下:
.
├── authauth
│ ├── forms.py
│ ├── __init__.py
│ ├── urls.py
│ ├── views.py
├── manage.py
├── static
│ ├── css
│ └── js
├── templates
│ ├── base.html
│ ├── home.html
│ └── login.html
└── tools
├── __init__.py
├── settings.py
├── urls.py
├── wsgi.py
tools.settings.py (我希望这些内容值得注意)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_URLCONF = 'tools.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#'DIRS': ["templates"],
'DIRS': [
BASE_DIR + '/templates',
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
工具/ urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views
from authauth.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('authauth.urls')),
url(r'^login/?$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name='login'),
url(r'^logout/$', views.logout, {'next_page': '/login'}),
]
authauth / views.py
from __future__ import unicode_literals
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required(login_url="login/")
def home(request):
return render(request,"home.html")
authauth / urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
authauth / forms.py
from django.contrib.auth.forms import AuthenticationForm
from django import forms
class LoginForm(AuthenticationForm):
username = forms.CharField(label="Username", max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'username'}))
password = forms.CharField(label="Password", max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'password'}))
模板/ base.html文件
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Auth Demo</title>
<!-- Core CSS - Include with every page -->
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Rocky Balbo</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Account
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/home">home</a>
</li>
<li>
<a href="#">Another action</a>
</li>
<li>
<a href="#">Something else here</a>
</li>
<li class="divider"></li>
<li>
<a href="#">Separated link</a>
</li>
<li class="divider"></li>
<li>
<a href="/logout">Logout</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
{% block content %}
{% endblock %}
<!-- Core Scripts - Include with every page -->
<script src = "{% static 'js/jquery.min.js' %}"></script>
<script src = "{% static 'js/bootstrap.min.js' %}"></script>
{% block javascript %}
{% endblock %}
</body>
</html>
模板/ home.html做为
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row">
<div class="jumbotron">
<h1>Hello</h1>
<p>You are on your Dashboard</p>
</div>
</div>
</div>
{% endblock %}
模板/ login.html的
{
% extends 'base.html' %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Please Sign In</h3>
</div>
<div class="panel-body">
<form method="post" action="{% url login %}">
{% csrf_token %}
<p class="bs-component">
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
</p>
<p class="bs-component">
<center>
<input class="btn btn-success btn-sm" type="submit" value="login" />
</center>
</p>
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
<script>
{% if not user.is_authenticated %}
$("ul.nav.navbar-nav.navbar-right").css("display","none");
{% endif %}
</script>
{% endblock %}
非常感谢帮助。
答案 0 :(得分:0)
尝试在名为tools的模板中创建一个文件夹,并将所有模板保存在该文件夹中 这是django文档中建议的方法 也不要忘记更改工具/ login.html的路径
答案 1 :(得分:0)