我正在尝试将网址转到用户个人资料的页面。我在网址中使用(?P<pk>\d+)
来执行此操作,但是我收到了错误。此外,如果我删除了在base.html中链接它的问题就会消失。但后来我在nabber中没有链接...
错误:
NoReverseMatch at /
Reverse for 'user_profile' with no arguments not found. 1 pattern(s) tried: ['user/(?P<pk>\\d+)/$']
In template /Users/garrettlove/Desktop/evverest/templates/base.html, error at line 23
Reverse for 'user_profile' with no arguments not found. 1 pattern(s) tried: ['user/(?P<pk>\\d+)/$']
13
14 <body>
15
16 <nav>
17 <div class="container">
18 <a class="brand" href="{% url 'index' %}">Evverest</a>
19
20 <div class="navbar">
21 <a class="nav-link btn" href="{% url 'index' %}">Home</a>
22 {% if user.is_authenticated %}
23 <a class="nav-link" href="{% url 'users:user_profile' %}">New Color Set</a>
24 <a class="nav-link" href="{% url 'users:user_logout' %}">Logout</a>
25 {% else %}
26 <a class="nav-link" href="{% url 'users:user_login' %}">Login</a>
27 <a class="nav-link" href="{% url 'register' %}">Register</a>
28 {% endif %}
29 </div>
30 </div>
31 </nav>
32
33 <div class="container">
它突出显示上述代码中的第23行作为错误。
这是我的代码:
app urls:
from django.conf.urls import url
from users import views
app_name = 'users'
urlpatterns = [
url(r'^(?P<pk>\d+)/$',views.UserProfileView.as_view(),name='user_profile'),
url(r'^(?P<pk>\d+)/edit$',views.UserEditProfileView.as_view(),name='user-profile-edit'),
url(r'^login/$',views.user_login,name='user_login'),
url(r'^logout/$',views.user_logout,name='user_logout',kwargs={'next_page':'/'}),
url(r'^register/$',views.register,name='register'),
]
base.html文件
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<title>Evverest</title>
<meta name"viewport" content="width=device-width, initial-scale=1">
<meta charset="uft-8">
<link rel="shortcut icon" href="/images/favicon.ico">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<nav>
<div class="container">
<a class="brand" href="{% url 'index' %}">Evverest</a>
<div class="navbar">
<a class="nav-link btn" href="{% url 'index' %}">Home</a>
{% if user.is_authenticated %}
<a class="nav-link" href="{% url 'users:user_profile' %}">New Color Set</a>
<a class="nav-link" href="{% url 'users:user_logout' %}">Logout</a>
{% else %}
<a class="nav-link" href="{% url 'users:user_login' %}">Login</a>
<a class="nav-link" href="{% url 'register' %}">Register</a>
{% endif %}
</div>
</div>
</nav>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
应用视图:
from django.shortcuts import render
from users.forms import UserForm,UserProfileForm
from users.models import UserProfileInfo
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
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("Account now active")
else:
print("Login Unsuccessful")
return HttpResponse("Username and/or password are not correct")
else:
return render(request,'login.html',{})
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(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 = UserProfileForm()
return render(request,'register.html',{
'user_form':user_form,
'profile_form':profile_form,
'registered':registered
})
@login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))
class HomeView(ListView):
model = UserProfileInfo
ordering = ['-join_date']
class UserProfileView(DetailView):
model = UserProfileInfo
class UserEditProfileView(LoginRequiredMixin,UpdateView):
login_url = '/login/'
redirect_field_name = '/users_detail.html'
form_class = UserProfileForm
model = UserProfileInfo
项目网址:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from users import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.HomeView.as_view(),name='index'),
url(r'^user/',include('users.urls',namespace='users')),
url(r'^login/',views.user_login,name='login'),
url(r'^logout/',views.user_logout,name='logout',kwargs={'next_page':'/'}),
url(r'^register/',views.register,name='register'),
]
答案 0 :(得分:1)
将您的第一个代码区第23行更改为
<a class="nav-link" href="{% url 'users:user_profile' user.id %}">New Color Set</a>