Django中的Success_url

时间:2019-06-27 04:05:32

标签: django

我是Django(版本2.2.2)的新手,可以使用一些帮助。我正在创建一个非常简单的包含表单的网页。在views.py文件中为表单创建CreateView时,我包含了success_url属性。用户输入详细信息并单击提交后,会将其重定向到主页。除了success_url之外,其他一切似乎都运行良好。它不会重定向到首页,而是会导致错误。有谁知道如何做到这一点?

models.py中的学生模型:

from django.db import models

class Student(models.Model): 
    name = models.CharField(max_length = 25) 
    age = models.IntegerField() 
    email = models.EmailField() 
    created_date = models.DateTimeField(auto_now = True) 
    def __str__(self): 
        return self.name 

views.py:

from django.shortcuts import render
from django.views.generic import CreateView 
from app.forms import StudentForm

class StudentView(CreateView): 
    template_name = 'create_student.html' 
    form_class = StudentForm 
    success_url = '/home/'      

app.urls文件:

from django.urls import path 
from app.views import StudentView 

urlpatterns = [
    path('create_student', StudentView.as_view(), name = 'create_student'),
] 

project.urls文件:

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

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

HTML文件:

<form method="POST">
    {% csrf_token %}
    {{form.as_p}}
    <button> Submit </button>
</form>

0 个答案:

没有答案