django中的用户登录视图工作不正常?
注册页面后我有一个登录页面登录页面中的问题当我使用有效凭据登录时,它会重定向到仪表板,当我提供无效凭据时,它也会使用登录表单中键入的最后一个有效凭据重定向到仪表板。我不明白为什么会这样 我的观点django中的用户登录视图工作不正常?,django,django-templates,django-views,Django,Django Templates,Django Views,注册页面后我有一个登录页面登录页面中的问题当我使用有效凭据登录时,它会重定向到仪表板,当我提供无效凭据时,它也会使用登录表单中键入的最后一个有效凭据重定向到仪表板。我不明白为什么会这样 我的观点 def user_register(request): if request.method == 'POST': # if there is a post request in the form user_form = UserForm(data=request.POST) #f
def user_register(request):
if request.method == 'POST': # if there is a post request in the form
user_form = UserForm(data=request.POST) #first of all it is a user_form will be posted details present in the user_form
user_requirement_form = UserRequirementForm(data=request.POST)# after posting the details of the user_form post the details
if user_form.is_valid() and user_requirement_form.is_valid():
# if user_form & user_requirement form is valid
User = user_form.save()#if form is valid save
User.set_password(request.POST['password'])
User.save()
user_requirement = user_requirement_form.save(commit=False)
# Set user
user_requirement.user = User
user_requirement.save()
user_requirement_form.save_m2m()
messages.success(request,('Project saved successfully'))
return render(request,'home1.html')
else:
messages.warning(request, 'Please correct the errors above')
else:
user_form = UserForm()
user_requirement_form = UserRequirementForm()
return render(request,'register.html', {'user_form': user_form, 'requirements_form': user_requirement_form})
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
messages.success(request, 'You are now logged in')
return render(request,'dashboard.html')
else:
messages.error(request,'Invalid Credentials')
return render(request,'dashboard.html')
else:
return render(request, 'login.html')
def dashboard(request):
return render(request, 'dashboard.html',);
My dashboard.html:
<details>
<summary class="summary ml-4">User Details</summary>
<p class="p1 ml-4">The contents that are selected by the user</p>
<p class="p2 ml-4">{{request.user.user_requirement.user}}</p>
</details>
<p class="p3 ml-4 mt-4"><b>Room:<br></b>{{request.user.user_requirement.room}}</p>
<p class="p4 ml-4"><b>GOAL:</b>
{% for goal in request.user.user_requirement.goal.all %}
<br>{{goal}}
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
<p class="p5 ml-4"><b>FURNITURE:</b><br>{{request.user.user_requirement.furniture}}</p>
<p class="p4 ml-4"><b>DESIGN:</b>
{% for design in request.user.user_requirement.design.all %}
<br>{{design}}
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
返回呈现(请求'login.html')
应该是主函数的一部分
因为如果您处于条件(method==Post)且用户不存在,
主函数将不会返回返回呈现(请求'login.html')
应该是主函数的一部分
因为如果您处于条件(method==Post)且用户不存在,
主函数将不会返回试试这个。如果这引起任何问题,请发表评论。您还应该使用redirect()将用户重定向到仪表板。从检查用户的其他部分删除此“return render(request,'dashboard.html')”。即使用户不存在,您也将重定向到仪表板。此外,请在成功登录后使用should use redirect()将用户重定向到仪表板。如果我删除视图modsy.views.login未返回HttpResponse对象。它没有返回任何结果。出现此错误是因为“return render(request,'login.html')”位于主条件的其他部分,并且函数不返回任何内容。您不需要“else”。将这一行放在主函数中。我没有让您@sandeepremove这个“return render(request,'dashboard.html')”从您检查用户的其他部分。您正在重定向到仪表板,即使用户不存在。还可以使用should-use redirect()在成功登录后将用户重定向到仪表板。如果我删除该选项,则视图modsy.views.login没有返回HttpResponse对象。相反,它没有返回任何对象。出现此错误是因为“return render(request,'login.html')”在主条件的其他部分,而函数没有返回任何内容。您不需要“else”。将这一行放在主函数中。当我使用返回重定向(“dashboard”)重定向到“dashboard”未找到的“dashboard”的反向时,我没有得到@SandeYes“dashboard”不是有效的视图函数或模式名称。获取此错误此答案将修复当前问题。不要在这里使用重定向。如果要使用重定向,请使用正确的url名称或显示您的url.py。但现在,请按原样使用代码。OP没有重定向到任何地方,他正在呈现dashboad模板。这真的不是同一件事(实际上肯定不是应该做的)@sssss,你能显示错误的屏幕截图吗,因为我可以在你的url中看到“dashboard”。@NalinDobhal这就是
redirect()
将要做的(当然除了url名称的命名空间)。当我使用return redirect('dashboard')时是的用于重定向到“未找到仪表板”的仪表板反转“dashboard”不是有效的视图函数或模式名称。获取此错误此答案将修复当前问题。不要在这里使用重定向。如果要使用重定向,请使用正确的url名称或显示您的url.py。但现在,请按原样使用代码。OP没有重定向到任何地方,他正在呈现dashboad模板。这真的不是同一件事(实际上肯定不是应该做的)@sssss,你能显示错误的屏幕截图吗,因为我可以在你的url中看到“dashboard”。@NalinDobhal这就是redirect()
将要做的(当然除了url名称的名称空间)。
<form action="{% url 'modsy:login' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label for="password2">Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<input type="submit" value="Login" class="btn btn-secondary btn-block">
</form>
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
path('rooms/', views.project1, name='project1'),
path('goals/', views.project2, name='project2'),
path('furniture/', views.project3, name='project3'),
path('styles/', views.project4, name='project4'),
path('register/', views.home, name='home'),
path('user_register/', views.user_register, name='user_register'),
path('login/', views.login,name='login'),
path('dashboard/', views.dashboard,name='dashboard')
]
from django.http import HttpResponseRedirect
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
messages.success(request, 'You are now logged in')
return HttpResponseRedirect(reverse('dashboard')) # Edit
else:
messages.error(request,'Invalid Credentials')
# return render(request,'dashboard.html') ## removed 1
# else: ## removed 2
return render(request, 'login.html')