在成功提交在redirect()
视图中找到的表单后,我尝试使用index
方法重定向到我的register
视图。
我有以下urls.py
:
from django.contrib import admin
from django.urls import path
from users import views as usersViews
urlpatterns = [
path('admin/', admin.site.urls),
path('', usersViews.index, name = 'index'),
path('register/', usersViews.register, name = 'regsister'),
]
和以下views.py
:
from django.shortcuts import render, redirect
from .forms import RegistrationForm
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('signed up')
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save() # save form? to db?
username = form.cleaned_data.get('username')
email = form.cleaned_data.get('email')
return redirect("index") #this statement is not working
else:
form = RegistrationForm()
return render(request, 'users/registration.html', {'registrationForm':form})
提交表格后,我将被重定向到“ /register/index.html”而不是“ /”(索引页)。有人可以帮忙吗?
答案 0 :(得分:0)
反向尝试:
from django.urls import reverse
return redirect(reverse('app_name:view_name'), {'registrationForm':form})