Django: Reverse for '' not found. '' is not a Valid view Function or Pattern name

Asked

Viewed 5,459 times

3

I am trying to put a url based on my views.py but it returns me the error:

Reverse for 'create_student' not found. 'create_student' is not a Valid view Function or Pattern name.

py urls of the project:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('registrations.urls'))
    ]

py urls.of the app:

from django.urls import path
from .views import list_students, create_student, student_description
app_name = 'registrations'

urlpatterns = [
    path('', list_students, name='list_students'),
    path('cadastrar', create_student, name='create_student'),
    path('passo4/<int:id>', student_description, name='student_description'),
    ]

views.py of the app

from django.shortcuts import render,  redirect
from .models import Student, StudentForm, StudentDescriptionForm, ParentStudent, ParentStudentForm

def list_students(request):
    students = Student.objects.all()
    return render(request, 'students.html', {'students': students} )

def create_student(request):
    form = StudentForm(request.POST or None)

    if form.is_valid():
        form.save()
        return redirect('list_students')
    
    return render(request, 'student-form-registration.html', {'form': form})

def student_description(request):
    student = Student.objects.get(id=id)
    form = StudentDescriptionForm(request.POST or None, instance=student)

    if form.is_valid():
        form.save()
        return redirect('list_students')

    return render(request, 'student-description-form.html', {'form': form, 'student': student})

student html.:

{% load bootstrap4%}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    {% bootstrap_css%}
</head>
<body>
    <ul>
        {% for student in students %}
        <a href="">
            <li>{{student.name}}</li>
        </a>
        {% endfor %}
    </ul>

    <a href="{% url 'create_student' %}"> nova incrição </a>
</body>
</html>

1 answer

3


In the archive urls.py of your app is missing the slash in url by name create_student.

Do so:

from django.urls import path
from .views import list_students, create_student, student_description
app_name = 'registrations'

urlpatterns = [
    path('', list_students, name='list_students'),
    path('cadastrar/', create_student, name='create_student'),
    path('passo4/<int:id>', student_description, name='student_description'),
    ]

Add a namespace to his URLConf as follows:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('registrations.urls', namespace='registrations'))
    ]

and use in template:

<a href="{% url 'registrations:create_student' %}"> nova incrição </a>
  • Then Thiago , I even noticed that I was missing the " / " but this did not solve my problem, when access: localhost:8000 I am directed to a page . html that I created, however, every time I put <a href="{% url 'create_student' %}"> nova incrição </a> and try to update, I get the following error: Exception Value: Reverse for 'create_student' not found. 'create_student' is not a Valid view Function or Pattern name. , I’ve looked in some places and always do that way

  • Oh man, that way it worked out man, thank you so much for the help

  • The Django uses the namespace as a way to differentiate that two urls of different applications but having the same name are considered equal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.