简体   繁体   中英

Django 1.6 301 url redirect not working

I'm trying to do aa 301 redirect using the django redirect app but I still keep getting the 404 page when I visit the old url. I followed the documentation https://docs.djangoproject.com/en/1.6/ref/contrib/redirects/ and added a old and new url in database but it's still not working.

Is there any other way of doing a 301 redirection

I have this url http://localhost:8000/doclistings/?speciality=Dentist and I want it to be redirected to 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)

You don't really have to do redirect. Correct me if I'm wrong but you want to access the value of the speciality and if the user key in Dentist you want it to be Dentists .

I think the correct way to ensure the key, value of a GET request get filled in properly is by form validation. There are many ways of doing form validation, you can do it with javascript or by django form.

However, a quick hack around your problem to pass in some logic into your code, that if the value of speciality == 'doctor' , you set it to doctors instead

For instance,

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)

Let me know if this works for you

Cheers, BioBirdMan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM