【问题标题】:Getting an error 'redirect' on views.py of django project在 django 项目的 views.py 上出现错误“重定向”
【发布时间】:2019-05-31 10:26:53
【问题描述】:

所有注册表单都运行良好。问题出在函数 addUser 的重定向上。我在重定向时遇到错误。我正在尝试将数据保存在管理面板中,但我被卡住了。

 from django.shortcuts import render,redirect
from django.http import HttpResponse
from .forms import RegistrationForm
from .models import RegistrationData
#from django.contrib.auth.form import UserCreationForm


# Create your views here.
def index(request):
return render(request, "Yatri/home.html")

def SignUp(request):
 context= {"form":RegistrationForm} 
 return render(request,"Yatri/register.html",context)

def addUser(request):
  form=RegistrationForm(request.POST)
  if form.is_valid():
        register=RegistrationData(username=form.cleaned_data['username'],
                                  password=form.cleaned_data['password'],
                                  email=form.cleaned_data['email'],
                                  phone=form.cleaned_data['phone'],

        register.save()


  return redirect('index')      

我希望将用户名、密码、电子邮件和电话保存在数据库中,但我收到无法访问站点的错误。

网址.py

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

urlpatterns = [
path('', views.index, name='index'),
path('Signup/',views.SignUp,name='Signup'),
path('addUser/',views.addUser,name='addUser'),
]

【问题讨论】:

  • 显示您的网址。
  • 我已经在@DanielRoseman 上面添加了网址
  • 你的命令行有错误吗?
  • 是的,我得到的错误是:文件“C:\Users\Aakash\dev\cfehome\MeroYatra\Yatri\urls.py”,第 3 行,在 中来自 .导入视图文件“C:\Users\Aakash\dev\cfehome\MeroYatra\Yatri\views.py”,第 27 行 return redirect('index') ^ SyntaxError: invalid syntax

标签: html django python-3.x django-2.1


【解决方案1】:

addUser 视图中缺少括号:

def addUser(request):
  form=RegistrationForm(request.POST)
  if form.is_valid():
        register=RegistrationData(username=form.cleaned_data['username'],
                                  password=form.cleaned_data['password'],
                                  email=form.cleaned_data['email'],
                                  phone=form.cleaned_data['phone'],)

        register.save()


  return redirect('index') 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2018-03-17
    相关资源
    最近更新 更多