NoReverseMatch用于用户个人资料视图

时间:2019-09-07 11:23:37

标签: django django-views django-urls django-2.2

我需要为用户提供两个不同的视图:

  • 视图 user_details ,其中显示模型 User ;
  • 的详细信息
  • 视图 user_profile ,其中显示模型 UserProfile
  • 的详细信息

models.py

from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse

class UserProfile(models.Model):
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name='profile_user',
    )
    many_other_fields....

views.py

from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, HttpResponseRedirect, redirect, render

from .models import UserProfile

def user_details(request, username):
    user_details = get_object_or_404(User, username=username)
    template = 'usermanager/reading/user_details.html'
    context = {
        'user': user_details,
    }
    return render(request, template, context)

def user_profile(request, username):
    profile = get_object_or_404(UserProfile, username=profile_user.username)
    template = 'usermanager/reading/user_profile.html'
    context = {
        'user_profile': profile,
    }
    return render(request, template, context)

urls.py

path('account-manager/<username>/', views.user_details, name="user_details"),
path('account-manager/<username>/profile/', views.user_profile, name="user_profile"),

template.html

<a href="{% url 'user_details' username=user.username %}">User Details</a>
<a href="{% url 'user_profile' username=userprofile.profile_user.username %}">User Profile</a>

使用这种方法,我看到以下错误:

  

在/ account-manager / max /处的NoReverseMatch /为'user_profile'反向   找不到带有关键字参数'{'username':''}'的关键字。 1个图案   尝试过:['account \ -manager \ /(?P [^ /] +)\ / profile \ / $']

我该如何解决这个问题?您能建议我另一种方法吗?

完整跟踪

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/account-manager/

Django Version: 2.2.5
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'kernel',
 'usermanager']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/max/Django/area-test/app_usermanager_dev/app_usermanager/usermanager/templates/usermanager/backend_nav.html, error at line 11
   Reverse for 'user_profile' with keyword arguments '{'username': ''}' not found. 1 pattern(s) tried: ['account\\-manager\\/(?P<username>[^/]+)\\/profile\\/$']
   1 : {% load static %}
   2 : <div class="px-2 pt-2 navbar sticky-top">
   3 : 
   4 :   <div class="" id="nav_container">
   5 : 
   6 :     <nav class="navbar flex-column navbar-dark" id="nav_single">
   7 :         <p class="nav-item"><a id="nav_link" href="/"><i class="fas fa-home"></i> Site Home</a></p>
   8 :         <hr>
   9 :         <p class="nav-item"><a id="nav_link" href="{% url 'dashboard' %}"><i class="fas fa-tachometer-alt"></i> Dashboard</a></p>
   10 :         <p class="nav-item"><a id="nav_link" href="{% url 'user_details' username=user.username %}"><i class="fas fa-user-circle"></i> User Details</a></p>
   11 :         <p class="nav-item"><a id="nav_link" href=" {% url 'user_profile' username=userprofile.user.username %} "><i class="fas fas fa-id-card"></i> User Profile</a></p>
   12 :         <p class="nav-item"><a id="nav_link" href="#"><i class="fas fa-clipboard-list"></i> Buyed Products</a></p>
   13 :     </nav>
   14 : 
   15 : 
   16 : 
   17 :     <hr>
   18 :     <nav id="nav_single">
   19 :       <p class="text-center"><a class="btn btn-warning btn-sm" href="{% url 'logout' %}"><i class="fas fa-power-off"></i></a></p>
   20 :     </nav>
   21 : 


Traceback:

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/max/Django/area-test/app_usermanager_dev/app_usermanager/usermanager/views.py" in dashboard
  100.     return render(request, template)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render
  171.                     return self._render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  188.             return template.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render
  173.                 return self._render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/template/defaulttags.py" in render
  443.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/urls/base.py" in reverse
  90.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "/home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  673.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /account-manager/
Exception Value: Reverse for 'user_profile' with keyword arguments '{'username': ''}' not found. 1 pattern(s) tried: ['account\\-manager\\/(?P<username>[^/]+)\\/profile\\/$']

1 个答案:

答案 0 :(得分:0)

当您有一个LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/NewFolder/MYfile.csv' INTO TABLE discounts(title,expired_date,amount) FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n' IGNORE 1 ROWS; 实例时,错误地使用了反向related_name来访问user。假设您在UserProfile的上下文中有一个UserProfile实例,则模板应为:

userprofile

<a href="{% url 'user_profile' username=userprofile.user.username %}">User Profile</a>适用于 reverse 关系,因此,如果您有一个名为related_name的{​​{1}}对象,那么User将为您提供相关的user实例,但是当您有user.user_profile时,相关的UserProfile将位于UserProfile