gpt4 book ai didi

python - NoReverseMatch at/login - LOGIN_URL 或反向功能出错?

转载 作者:行者123 更新时间:2023-12-01 06:25:23 25 4
gpt4 key购买 nike

我正在 Django 中开发一个应用程序。

我正在开发用户身份验证。

我在路径中有一个 registration.htmllogin.html 模板:模板>身份验证

一切,包括注册功能,都工作正常,但是当我尝试访问登录模板时,浏览器返回:

NoReverseMatch at /login

'app' is not a registered namespace

我敢打赌问题出在我在 settings.py 中添加的用于启用身份验证系统的 LOGIN_URL(我正在遵循教程)。事实上,所有其他 View 都工作正常,只有指向 login.html 的 View 却不然。

以下是我与身份验证系统相关的所有内容:

在我的settings.py中:

LOGIN_URL = '/login'

在我的base.html中:

          {% if user.is_authenticated %}

<li class="nav-item dropdown">

<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">{{ user_form.username }}</a>

<div class="dropdown-menu">

<a class="dropdown-item" href="">profilo</a>
<a class="dropdown-item" href="{% url 'logout' %}">Log out</a>

</div>

</li>

{% elif not user.is_authenticated %}

<li class="nav-item dropdown">

<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Login</a>

<div class="dropdown-menu">

<a class="dropdown-item" href="{% url 'registration' %}">Registrati</a>
<a class="dropdown-item" href="{% url 'login' %}">Accedi</a>

</div>

</li>

{% endif %}

在我的身份验证> login.html中:

{% extends 'base.html'%} <!-- vuol dire inserisci qui la navigation toolbar contenuta in base -->

{% block content %}

<h1>Login</h1>

<br>

<div class="jumbotron">

<form action="{% url 'app:login' %}" method="post">
{% csrf_token %}

<label for="username">Username:</label>
<input type="text" name="username" value="" placeholder="nome utente">

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

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

</form>

</div>



{% load static %} <!-- Qui il tag è obbligatorio nonostante sia stato inserito dentro base.html -->

<!-- CSS -->
{% comment %} <link rel="stylesheet" type="text/css" href={% static "css/file.css" %}> {% endcomment %}

<!-- Javascript -->
{% comment %} <script type="text/javascript" src={% static "js/file.js" %}></script> {% endcomment %}

{% endblock %}

在我的 app > urls.py 中,urlpatterns 列表中:

path('authentication/registration', views_users_authentication.registration, name="registration"),
path('login', views_users_authentication.user_login, name="login"),

在我的项目> urls.py中,urlpatterns列表中:

path('admin/', admin.site.urls),
path('', include('app.urls')),

然后我有一个单独的表来包含与身份验证系统相关的 View 函数,即 views_users_authentication.py ,其中包含:

def registration(request):

registered = False

# se l'utente ha lanciato il post
if request.method=="POST":

print("post eseguito!")
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST)

# condizione di validità del form
if user_form.is_valid() and profile_form.is_valid():

print("form validi!")

user = user_form.save()
user.set_password(user.password) # questa linea hasha la pasword
user.save()
# registra l'utente

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

registered=True

print("Utente registrato con successo!")

# condizione per registrare l'utente
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
print("Acquisita la fotografia dell'utente!")

profile.save()
# attenzione al salvataggio dei form e dei modelli che sono due cose diverse
# registra le info aggiuntive



else:
print("Registrazione fallita:")
print(user_form.errors, profile_form.errors)

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

context_dict = {'user_form':user_form, 'profile_form':profile_form, 'registered':registered}

return render(request, 'authentication/registration.html', context_dict)


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('home'))

else:
HttpResponse("Account non attivo")

else:
print("qualcuno ha cercato di loggarsi e ha fallito")
print("Username: {} and password {}".format(username,password))
return HttpResponse("Inseriti parametri non validi per il login!")

else:
return render(request, "authentication/login.html", {})

最佳答案

在您的 login.html 中,您应该仅使用 login 作为网址名称,而不是 app:login:

<form action="{% url 'login' %}" method="post">

因为您没有在 urlpatterns.py 文件中指定 namespace 。如果您想使用 app 命名空间,您可以像这样更改 urlpattern:

path('', include('app.urls', namespace='app')),

关于python - NoReverseMatch at/login - LOGIN_URL 或反向功能出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60173679/

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