如何修复NoReverseMatch?

时间:2019-07-15 13:04:58

标签: python forms django-views

当我单击提交时,我想返回当前页面。

无论我是否重定向,只要单击提交,但仍会报告NoReverseMatch,但已添加数据库。

urls.py

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

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

app.urls.py

    from django.urls import path
    from . import views

    app_name = 'student'
    urlpatterns = [
        path('', views.index, name='index'),
    ]

views.py

    from django.shortcuts import render, redirect

    from .forms import StudentForm
    from .models import Student

    def index(request):
        students = Student.get_all()
        if request.method == 'POST':
            form = StudentForm(request.POST)
            if form.is_valid():
                form.save()
                #return redirect(reverse('index'))
        else
            form = StudentForm()

        context = {'students': students, 'form':form,}

        return render(request, 'index.html', context=context)

index.html

    <!DOCTYPE html>
    <html>
        <head>
            <title>学员管理系统</title>
        </head>
        <body>
            <h3><a href="/admin/">管理员</a></h3>
            <ul>
                {% for student in students %}
                <li>{{ student.name }} - {{ student.get_status_display 
    }}</li>
                {% endfor %}
            </ul>
            <hr/>
            <form  method="post">
                {% csrf_token %}
                {{ form }}
                <input type="submit" value="Submit" />
            </form>
        </body>
    </html>

the page like that

1 个答案:

答案 0 :(得分:0)

也许您忘记将应用程序添加到settings.py文件中的INSTALLED_APPS列表中了吗?