我试图在django上为我的电影项目添加一个登录功能,并显示此错误:
AttributeError at /signin 'str' object has no attribute 'POST'
这是模板,signin.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="container">
<h1>Sign-in</h1>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="mb-3">
<label class="form-label"></label>
<input class="form-control" type="text" name="u" placeholder="Enter your username">
<div class="form-text"></div>
</div>
<div class="mb-3">
<label class="form-label"></label>
<input class="form-control" type="text" name="p" placeholder="Enter your password">
<div class="form-text"></div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% if messages %}
{% for i in messages%}
<ul class='messages'>
<li>{{i}}</li>
</ul>
{% endfor %}
{% endif %}
</div>
{% endblock %}
路径
from django.urls import path
from movie import views
app_name='movie'
urlpatterns = [
path('signin',views.signin,name='signin'),
]
views.py
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,login,logout
from django.contrib import messages
def signin(request):
if(request.method=='POST'):
u=request.method.POST['u']
p=request.method.POST['p']
user=authenticate(username=u,password=p)
if user:
login(request,user)
return home(request)
else:
messages.error(request,'Invalid Credentials')
return render(request,'signin.html')
点击提交后显示错误...
1条答案
按热度按时间hrirmatl1#
它不是
request.method.POST
,而是**request.POST
**[Django-doc],所以:注意:您可以通过自定义模板和消息轻松自定义Django的**
LoginView
**[Django-doc],而无需实现自己的登录视图。这导致更少的样板代码,并使其更不易受到(安全)错误的影响。