简体   繁体   中英

How do I solve a Django NoReverseMatch Error?

How do I solve a Django NoReverseMatch Error?

When I visit basic_list, I get the error "Reverse for 'manage_account' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['manage_account/(?P[0-9]+)/$']" but all the other links are working.

models.py

from django.db import models

class Wine(models.Model):
    name = models.CharField(max_length=300)
    year = models.IntegerField()
    objects = models.Manager()

    def __str__(self):
        return str(self.pk) + ": " + self.name

class Account(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    objects = models.Manager()

    def __str__(self):
        return str(self.pk) + ": " + self.username

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('login', views.view_login, name='view_login'),
    path('basic_list', views.view_basic_list, name='view_basic_list'),
    path('manage_account/<int:pk>/', views.manage_account, name='manage_account'),
]

views.py

from django.shortcuts import render, redirect, get_object_or_404
from .models import *
from django.core.exceptions import ObjectDoesNotExist

def view_login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        account = Account.objects.get(username=username)   
        if password == account.password:
            return redirect('view_basic_list')
        else:
            return render(request, 'myapp/login.html', {'warning': "Invalid login"})
    else:
        return render(request, 'myapp/login.html')

def view_basic_list(request):
    wine_objects = Wine.objects.all()
    return render(request, 'myapp/basic_list.html', {'Wines':wine_objects})

def manage_account(request, pk):
    a = get_object_or_404(Account, pk=pk)
    return render(request, 'myapp/manage_account.html', {'a': a})

basic_list.html

{% extends 'myapp/base.html' %}
{% load static %}

{% block content %}
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <table class="table table-striped">
                        <thead>
                            <th scope="col"> Name </th>
                            <th scope="col"> Year </th>
                        </thead>
                        <tbody>
                            {% for d in wines %}
                            <tr>
                                <td> {{ d.name }} </td>
                                <td> {{ d.year }} </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                    <a href="{% url 'manage_account' pk=a.pk %}" class="btn btn-primary" role="button" aria-pressed="true">Manage Account</a> 
                    <a href="login" class="btn btn-outline-primary" role="button" aria-pressed="true">Log Out</a> 
                </div>
            </div>
        </div> 
{% endblock %}

manage_account.html

{% extends 'myapp/base.html' %}
{% load static %}

{% block content %}
<div class="card mx-auto">
    <div class="card-header">                    
        <h4>My Account</h4>  
    </div>                                      

    <div class="card-body">
        <div class="mb-3">
            <h5>Username:</h5>                                
                <h6>{{a.username}}</h6>
        </div>
    </div>
</div>
{% endblock %}

The other URLs are working fine. http://127.0.0.1:8000/basic_list shows a NoReverseMatch error.

We are not allowed to use Django's built-in user models and authentication system.

What should I do? Sorry, I am new to Django. Any suggestions would be greatly appreciated.

The problem is that you are using an a variable which is not passed to the basic_list.html template that is used on the view_basic_list view.

You should also pass the pk variable of the account to view_basic_list view. And pass the account to the template:

def view_basic_list(request, pk):
    a = get_object_or_404(Account, pk=pk)
    wine_objects = Wine.objects.all()
    return render(request, 'myapp/basic_list.html', {'Wines':wine_objects, 'a': a})

did you try this?

<a href="/manage_account/{{a.pk}}/" class="btn btn-primary" role="button" aria-pressed="true">Manage Account</a>

This way is not dynamic, so if you change your urls.py, you also have to change the href from the template. But it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM