gpt4 book ai didi

javascript - NoReverseMatch,反向登录未找到错误

转载 作者:可可西里 更新时间:2023-11-01 16:40:34 25 4
gpt4 key购买 nike

我正在尝试向 View 发送 AJAX 发布请求,但由于某种原因我收到了 Http500 错误。

这是 AJAX 函数:

function update_coins() {
$.ajax({
method: "POST",
url: "/coins",
data: {"coins": transaction},
success: function(data) {
console.log('yay')
$( ".status" ).contents()[0].textContent = "Balance: " + data.coins
}
})
};

然后我在上面复制并粘贴了必要的 Django CSRF 代码。

我得到的错误是:

django.urls.exceptions.NoReverseMatch: Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
"POST /coins HTTP/1.1" 500 59

我猜这与我的 urls.py 有关。

这是我的根应用程序的 urls.py:

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home$', views.home, name='home'),
url(r'^coins$', views.update_coins, name='coins'),
url(r'^shop', include('shop.urls', namespace='shop')),
url(r'^users/', include('users.urls', namespace='users')),
url(r'^oauth/', include('social_django.urls', namespace='social')),
]

这是我的商店应用程序的 urls.py:

urlpatterns = [
url(r'^', views.shop, name='shop'),
]

最后是我的用户应用程序的 urls.py

urlpatterns = [
url(r'^login/$', views.login_view, name='login'),
url(r'^logout/$', views.logout_view, name='logout'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^register/$', views.register, name='register'),
]

谢谢!

编辑 1:添加模板基本 html:

<!DOCTYPE html>

{% load staticfiles %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}{% endblock %}</title>
<link rel="shortcut icon" href="{% static 'assets/favicon.png' %}"/>
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
<link href="{% static 'css/font-awesome.css' %}" rel="stylesheet">
<link href="{% static 'css/bootstrap-social.css' %}" rel="stylesheet">
<link href="{% static 'base.css' %}" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Aladin" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Noto+Serif" rel="stylesheet">
{% block head-extras %}{% endblock %}
</head>
<body>
<nav class="navbar navbar-toggleable-md bg-white">
<div class="container">
<a href="/home" class="navbar-brand">
<h1 id="logo" class="nav-wel">Pomodoro</h1>
</a>
{% if request.user.is_authenticated %}
<span class="status">Balance:&nbsp;{{ request.user.profile.coins }}<img class="coin-img" src="{% static 'assets/coin.png' %}" height="40px" width="auto"></span>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle welcome nav-wel" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false" id="welcome">Welcome {{ user.get_username }}</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="/shop">Shop</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item">Leaderboard</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="/users/change-password">Change Password</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="/users/logout">Logout</a>
</div>
</li>
</ul>
{% endif %}
</div>
</nav>
{% block content %}
{% endblock %}
<script src="{% static 'js/jquery-3.2.1.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
<script src="{% static 'js/pomodoro.js' %}"></script>
</body>
</html>

店铺html:

<!DOCTYPE html>

{% extends "base.html" %}

{% load staticfiles %}

{% block title %}Pomodoro{% endblock %}

{% block content %}
{% if request.GET.success %}
<div class="modal" id="logout-modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="alert alert-success main" role="alert">
<p><b>You have successfully changed your password.</b></p>
</div>
</div>
</div>
</div>
</div>
{% endif %}
<audio id="coin-sound">
<source src="{% static 'assets/coin.mp3' %}">
</audio>
<div class="row centre-v">
<div class="card clock-card">
<div class="card-block">
<span class="motivation">Work hard, play hard.</span>
<div class="clock-timer" id="clock-timer">
<span class="minutes"></span>:<span class="seconds"></span>
</div>
<div class="options">
<button class="btn btn-success btn-lg start-pomodoro">Start Pomodoro</button>
<button class="btn btn-info btn-lg start-break1 hidden">Start Break</button>
<button class="btn btn-info btn-lg start-break2 hidden">Start Break</button>
<button class="btn btn-danger btn-lg reset hidden">Reset</button>
</div>
</div>
</div>
</div>
{% endblock %}

编辑 2:我的 views.py:

@login_required(login_url='/users/login')
def update_coins(request):
"""
Function based view for increasing/decreasing a user's coin balance
"""
try:
user = request.user
except User.DoesNotExist:
raise Http404("No user matches the given query.")
if request.is_ajax() and request.method=='POST':
amount = request.POST.get("coins", 5)
user.profile.coins += amount
user.save()
return JsonResponse({'coins': user.profile.coins})
else:
raise Http404

最佳答案

可能是您的用户应用 URL 覆盖了默认的登录反向 View 。尝试将 users app's urls.py 中的登录 url 名称重命名为其他名称,看看是否可行。

关于javascript - NoReverseMatch,反向登录未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275476/

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