Django: Reverse for '' not found. '' is not a valid view function or pattern name

1

I'm 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.

project urls.py:

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

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

app urls.py:

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'),
    ]

app views.py

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>
    
asked by anonymous 20.08.2018 / 21:43

1 answer

2

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

Do this:

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 your 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 it in the template:

<a href="{% url 'registrations:create_student' %}"> nova incrição </a>
    
21.08.2018 / 02:43