我在模板中使用登录表单时使用了Django身份验证,在我按下表单上的登录按钮后,页面刷新了,但没有登录我,并且没有显示{{context}}错误模板。
我的观点:
def login_view(request):
if request.method == 'POST':
all_categories = Categories.objects.all()
all_websites = Website.objects.all()
all_discounts = Discount.objects.order_by('-id')
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/',categories=all_categories,websites=all_websites,discounts=all_discounts)
else:
context = 'wrong username or password'
all_categories = Categories.objects.all()
all_websites = Website.objects.all()
all_discounts = Discount.objects.order_by('-id')
return redirect('/',categories=all_categories,websites=all_websites,discounts=all_discounts,context=context)
我的表单(header.html)
<div class="col-md-3 top-info-cart text-right mt-lg-4">
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}.</p>
<p><a href="{% url 'frontend:logout' %}">logout</a></p>
{% else %}
<ul class="cart-inner-info">
<li class="button-log">
<a class="btn-open" href="#">
<span class="fa fa-user" aria-hidden="true"></span>
</a>
</li>
</ul>
<p>{{context}}</p>
{% endif %}
<!---->
<div class="overlay-login text-left">
<button type="button" class="overlay-close1">
<i class="fa fa-times" aria-hidden="true"></i>
</button>
<div class="wrap">
<h5 class="text-center mb-4">Login Now</h5>
<div class="login p-5 bg-dark mx-auto mw-100">
<form action="{% url 'frontend:login' %}" method="post">
{% csrf_token %}
<div class="form-group">
<label class="mb-2">User name</label>
<input type="text" name="username" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your informations with anyone else.</small>
</div>
<div class="form-group">
<label class="mb-2">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1"type="password">
</div>
<button type="submit" class="btn btn-primary submit mb-4" value="Login">Sign In</button>
<p>Don't have an account? <a href="{% url 'frontend:signup' %}" style="color: orange;">SIGN UP </a></p>
</form>
</div>
<!---->
</div>
</div>
还有我的urls.py:
from django.urls import path
from .import views
app_name = 'frontend'
urlpatterns = [
#index
path('',views.index, name='index'),
#/discount/id
path('<id>/',views.single,name='single'),
#/category/category_name
path('category/<name>',views.category,name='category'),
#/signup
path('signup',views.signup,name='signup'),
#/logout
path('logout',views.logout,name='logout'),
#/login
path('login',views.login_view,name='login')
]
是因为我的身份验证错误或我的视图功能?我一直在尝试解决此问题,但仍然没有弄清楚。希望有人可以帮助我:)
答案 0 :(得分:-1)
尝试将表单的操作更改为action =“”:
<form action="" method="post">
您现在设置的方式我认为正在发生的事情是您的表单只是重新呈现登录视图。
向您的视图添加表单:
def login_view(request):
if request.method == 'POST':
form = YourLoginForm(data=request.POST)
if form.is_valid():
...
,您只需在模板中呈现表单即可。
<form action="" method="post">
{{ form.as_p }}
<input type="submit" value="submit"/>
</form>
另一个潜在的问题是视图的重定向:
return redirect('/'
在我的应用中,这会将我重定向到我的登录页面,因为我的主视图重定向到我的登录页面。登录成功后,也许可以尝试重定向到另一个视图。