Django Error ::全局名称'HttpRequestRedirect'未定义

时间:2013-06-11 11:14:17

标签: django django-models django-forms django-templates django-views

我收到了这个错误,我尝试了很多,但无法理解什么是错的。 我基本上试图进行注册注册,其中有几种形式需要填写

Request Method: GET
Request URL:    http://127.0.0.1:8000/gi/
Django Version: 1.5.1
Exception Type: NameError
Exception Value:    
global name 'HttpRequestRedirect' is not defined
Exception Location: /home/xx/Desktop/GoAmma/vendorAmmma/vreg/views.py in GIRegistration, line 42
Python Executable:  /home/xx/Desktop/forfte/bin/python
Python Version: 2.7.3

视图如下所示:

# Create your views here.
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from vreg.forms import RegistrationForm, LoginForm
from vreg.models import Vendor
from django.contrib.auth import authenticate, login, logout

def VendorRegistration(request):
    if request.user.is_authenticated():
        return HttpRequestRedirect('/profile/')
    if request.method=='POST':
        form= RegistrationForm(request.POST)
        if form.is_valid():
            user=User.objects.create_user(username=form.cleaned_data['username'], email= form.cleaned_data['emailadd'], password= form.cleaned_data['password'])
            user.save()
            vreg=Vendor(user=user)
            vreg.save()
            return HttpResponseRedirect('/profile/')
        else:
            return render_to_response('register.html', {'form':form}, context_instance=RequestContext(request))

    else:
        ''' user is not submitting form show them blank registration form'''
        form= RegistrationForm()
        context={'form':form}
        return render_to_response('register.html', context, context_instance=RequestContext(request))

@login_required
def Profile(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/')
    vreg= request.user.get_profile
    context={'vreg':vreg}
    return render_to_response('profile.html',context,context_instance=RequestContext(request))  

@login_required 
def GIRegistration(request):
    if request.user.is_authenticated():
        return HttpRequestRedirect('/profile/')
    if request.method=='POST':
        form= GIForm(request.POST)
        if form.is_valid():
            contact = form.save()
            contact.save()
            return HttpResponseRedirect('/profile/')
        else:
            return render_to_response('generalinfo.html', {'form':form}, context_instance=RequestContext(request))

    else:
        ''' user is not submitting form show them blank registration form'''
        form= GIForm()
        context={'form':form}
        return render_to_response('generalinfo.html', context, context_instance=RequestContext(request))

def LoginRequest(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method=='POST':
        form=LoginForm(request.POST)
        if form.is_valid():
            username=form.cleaned_data['username']
            password=form.cleaned_data['password']
            vreg= authenticate(username=username,password=password)
            if vreg is not None:
                login(request,vreg)
                return HttpResponseRedirect('/profile/')
            else:
                return render_to_response('login.html',{'form':form}, context_instance= RequestContext(request))
        else:
            return render_to_response('login.html',{'form':form}, context_instance= RequestContext(request))

    else:
        '''user not submitting form show the login form'''
        form= LoginForm()
        context={'form': form}
        return render_to_response('login.html',context,context_instance= RequestContext(request))

def LogoutRequest(request):
    logout(request)
    return HttpResponseRedirect('/')

1 个答案:

答案 0 :(得分:4)

HttpResponseRedirect而非HttpRequestRedirect

将此return HttpRequestRedirect('/profile/')更改为return HttpResponseRedirect('/profile/')

P.S。切勿在代码中使用硬编码的网址,请使用Reverse Resolution of URLs