1

我收到了这个错误,我尝试了很多,但不明白出了什么问题。我基本上是在尝试注册一个需要填写几张表格的注册

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('/')
4

1 回答 1

5

HttpResponseRedirect不是HttpRequestRedirect

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

PS 不要在你的代码中使用硬编码的 url,使用URL 的反向解析

于 2013-06-11T11:15:41.527 回答