Django 1.6 301网址重定向无法正常工作

时间:2015-01-25 07:16:15

标签: python django redirect

我正在尝试使用django重定向应用程序进行301重定向,但是当我访问旧网址时,我仍然继续获取404页面。我按照文档https://docs.djangoproject.com/en/1.6/ref/contrib/redirects/在数据库中添加了一个旧的和新的URL,但它仍然无效。

还有其他方法可以进行301重定向

我有此网址http://localhost:8000/doclistings/?speciality=Dentist,我希望将其重定向到http://localhost:8000/doclistings/?speciality=Dentists

urls.py

url(r'^doclistings/$', views.doclistings, name='doclistings'),

views.py

def doclistings(request):
    d = getVariables(request)
    if request.method == "GET":
        form = DropdownSelectionForm(request.GET)
        try:
            s_name = request.GET['speciality']
        except:
            s_name = None
        try:
            l_name = request.GET['language']
        except:
            l_name = None
        try:
            g_name = request.GET['gender']
        except:
            g_name = None

        d['s_name'] = s_name # adding these to the forms for the "selected" option
        d['l_name'] = l_name
        d['g_name'] = g_name


        try:
            doctors = filter_doctors(request=request, specialization=s_name, gender=g_name, language=l_name).order_by('-netlikes')

        except Exception:
            return error404(request)

    else:
        form = DropdownSelectionForm()

    d['doctors'] = doctors
    d.update({'form': form, 'languages': Language.objects.all()})
    return render_to_response('m1/doclistings.html',d)

1 个答案:

答案 0 :(得分:0)

你真的不需要做重定向。如果我错了,请纠正我,但是如果您希望Dentist中的用户密钥为Dentists,则您需要访问该专业的值。

我认为确保正确填写GET请求的键值的正确方法是通过表单验证。有很多方法可以进行表单验证,你可以用javascript或django表单来完成。

然而,快速解决您的问题,将一些逻辑传递到您的代码中,如果speciality == 'doctor'的值,则将其设置为doctors而不是

例如,

def doclistings(request):
    d = getVariables(request)
    if request.method == "GET":
        form = DropdownSelectionForm(request.GET)
        try:
            s_name = request.GET['speciality']
            if s_name == 'Dentist':
                s_name = 'Dentists'
        except:
            s_name = None
        try:
            l_name = request.GET['language']
        except:
            l_name = None
        try:
            g_name = request.GET['gender']
        except:
            g_name = None

        d['s_name'] = s_name # adding these to the forms for the "selected" option
        d['l_name'] = l_name
        d['g_name'] = g_name


        try:
            doctors = filter_doctors(request=request, specialization=s_name, gender=g_name, language=l_name).order_by('-netlikes')

        except Exception:
            return error404(request)

    else:
        form = DropdownSelectionForm()

    d['doctors'] = doctors
    d.update({'form': form, 'languages': Language.objects.all()})
    return render_to_response('m1/doclistings.html',d)

如果这对您有用,请告诉我

干杯, BioBirdMan