我只是不明白。我在这个错误上花了太多时间。请帮我指出这个问题。如果您需要任何其他信息,请与我们联系。
错误读取: NoReverseMatch at / login / here / 反向欢迎'参数'()'和关键字参数' {}'未找到。尝试了0种模式:[]
项目urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
# creating registered namespaces for each app
url(r'^login/', include('login_app.urls', namespace = "login_app")),
]
login_app / urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'here/', views.login_user),
]
login_app / views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from login_app.forms import LoginForm
from django.shortcuts import render_to_response # this is going to be depricated
from django.template import RequestContext
def login_user(request):
form = LoginForm()
logout(request) # logs out user upon reaching the /login/ page
user = password = ''
# if this is a POST request we need to process the form data
if request.POST:
# create a form instance and populate it with the data from the request:
form = LoginForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
user = form.cleaned_data['user'] # extracting the username from the form before authenticating
password = form.cleaned_data['password'] # extracting the password from the form before authenticating
user = authenticate(username=user, password=password) # authenticating
if user is not None:
login(request, user)
#return HttpResponseRedirect(reverse('dashboard_app:welcome'))
return HttpResponse("hi!")
else:
template = 'login.html'
state = "Your username and/or password are incorrect."
context = {'state': state,'form': form,}
return render(request, template, context)
template = 'login_app/login.html'
state = ''
context ={'state': state, 'form': form}
#return HttpResponse("hey!")
return render(request, template, context)
的login.html
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'login_app/css/login.css' %}">
<div class="container-fluid">
<div class="well">
{% csrf_token %}
{% crispy form %}<br>
<!--error handling-->
<p>{{ state }}</p>
</div>
</div>
{% endblock %}
login_app / forms.py
from django import forms
## importing crispy form utilities
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Div, ButtonHolder
from crispy_forms.bootstrap import FormActions, PrependedText
class LoginForm(forms.Form):
## user field
user = forms.CharField()
## password field
password = forms.CharField(
widget=forms.PasswordInput()
)
## Using crispy forms to create a form
helper = FormHelper()
helper.form_method = 'POST'
helper.form_show_labels = False
helper.layout = Layout(
Div(
PrependedText('user', '<i class="fa fa-user fa-fw"></i>', placeholder='Username'),
PrependedText('password', '<i class="fa fa-lock fa-fw"></i>', placeholder='Password', autocomplete='off'),
style='padding:0px;',
css_class='col-md-3'
),
Div(
ButtonHolder(
Submit('submit','Login'), css_class='col-md-12'
), css_class='row'
)
)
答案 0 :(得分:0)
所以为了将来的参考,这里发生的事情......
base.html
(我没有发布)中有这一行。
<a class="navbar-brand" href="#">Clinical Molecular Incident Reporting System</a> <!-- {% url 'login_app:welcome' %}-->
这里的错误是我认为html <!---->
的评论也适用于Django的内联渲染语法{%%}
,但它没有(回头看看,我不知道为什么我认为)。删除了html评论和中提琴!我花了一大笔时间来解决这个问题。希望这对以后的人有用。
感谢@Transformer努力解决我的问题。