У меня страница входа после страницы регистрации, проблема на странице входа, когда я вхожу с действительными учетными данными, она перенаправляет на панель мониторинга, а когда я даю неверные учетные данные, она также перенаправляет на панель управления с последними действительными учетными данными, введенными в поле Форма входа Я не понимаю, почему это так Мои views.py
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',);
Моя панель инструментов. 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>
Моя форма. html:
<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>
URLS.py:
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')
]