gpt4 book ai didi

python - Django 2.13 : NoReverseMatch at/basic_app/user_login/

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:46 25 4
gpt4 key购买 nike

我几乎完成了 Udemy 上的 Web 开发类(class),但在最后一步绊倒了。我真的希望有人能够帮助我,因为这似乎是我正在努力解决的一件非常简单的事情。

问题似乎出在用户单击“登录”链接时触发的 HttpResponseRedirect(reverse('index'))。

非常感谢!

错误在 http://127.0.0.1:8000/basic_app/user_login/ 触发

NoReverseMatch at /basic_app/user_login/
Reverse for 'basic_app/user_login' not found. 'basic_app/user_login' is not a valid view function or pattern name.

Error during template rendering
In template /Users/maxa/Dropbox/workspace/2018/projects/python-django-udemy-bootcamp/myplayground/Django_level_5/learning_users/templates/basic_app/base.html, error at line 6

项目的urls.py:

from django.contrib import admin
from django.conf.urls import url, include
from django.urls import path
from basic_app import views

urlpatterns = [
url(r'^$',views.index, name='index'),
path('admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
]

应用程序的 urls.py (basic_app/urls.py):

from django.conf.urls import url
from basic_app import views

# TEMPLATE RULES
app_name = 'basic_app'

urlpatterns = [
url(r'^register/$',views.register,name='register'),
url(r'^user_login/$',views.user_login,name='user_login'),
]

应用的 views.py (basic_app/views.py):

from django.shortcuts import render
from basic_app.forms import UserForm, UserProfileInfoForm

from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required

# Create your views here.

def index(request):
return render(request,'basic_app/index.html')

def register(request):
registered = False

if request.method == "POST":
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST)

if user_form.is_valid() and profile_form.is_valid():

user = user_form.save()
user.set_password(user.password)
user.save()

profile = profile_form.save(commit=False)
profile.user = user

if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']

profile.save()

registered = True

else:
print(user_form.errors,profile_form.errors)

else:
user_form = UserForm()
profile_form = UserProfileInfoForm()

return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,'registered':registered})

def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')

user = authenticate(username=username,password=password)

if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("Your account is not active.")
else:
print("Someone tried to log in and failed")
print("Username: {}. Password: {}".format(username,password))
return HttpResponse("invalid login details supplied!")

else:
return render(request,'basic_app/login.html',{})

@login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))

应用程序的 base.html (templates/basic_app/base.html):

<!DOCTYPE html>
<html>
<head>
<title></title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

</head>
<body> <nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container"><ul class="nav navbar-nav">
<li><a href="{% url 'index' %}" class="navbar-brand" class="nav-link">Home</a></li> <li class="nav-item"><a href="{% url 'admin:index' %}" class="nav-link">Admin</a></li>
<li class="nav-item"><a href="{% url 'basic_app:register' %}" class="nav-link">Register</a></li>

{% if user.is_authenticated %}

<li><a href="{% url 'logout' %}" class="nav-link">Log Out</li>

{% else %}

<li><a href="{% url 'basic_app:user_login' %}" class="nav-link">Log In</a></li>

{% endif %}


</ul></div>
</nav>

<div class="container">

{% block body_block %}
{% endblock %}

</div>

</body>
</html>

应用程序的 login.html (templates/basic_app/login.html):

{% extends 'basic_app/base.html' %}

{% block body_block %}

<div class="jumbotron">

<h1>Please login</h1>
<form action="{% url 'basic_app/user_login' %}" method="post">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username" value="" placeholder="Enter Username">

<label for="password">Password:</label>
<input type="password" name="password">

<input type="submit" name="" value="Login">
</form>


</div>

{% endblock %}

非常感谢!

最佳答案

您在 templates/basic_app/login.html 表单的 action 属性中有错字。将 basic_app/user_login 更改为 basic_app:user_login

<form action="{% url 'basic_app:user_login' %}" method="post">

关于python - Django 2.13 : NoReverseMatch at/basic_app/user_login/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53717825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com