简体   繁体   中英

Post method not working after redirect in Django

I am trying to create a system where certain pages can't be accessed unless the request is from a specific location. I currently have everything working except after the location is checked and the request is redirected to the next view, the form on that next view isn't posting properly. It's still trying to post the location data form.

You'll see what I mean more in my views.py :

def add(request):
    if request.method == 'POST':
        form = StudentModelForm(request.POST)
        if form.is_valid():
           form.save()
           return HttpResponseRedirect('/')
    else:
        form = StudentModelForm()
    context_data = {'form': form}
    return render(request, 'add.html', context_data)


def location_check_add(request):
    if request.method == 'POST' and 'lat' in request.POST:
        user_lat = request.POST.get('lat')
        user_lon = request.POST.get('lon')
        if good_location(user_lat,user_lon):
            return add(request)
        else:
            return render(request, 'location.html')
    return render(request, 'checking.html')

So I'm trying to redirect to the add() view if good_location is true. It redirects properly but then when a user tries to post the form on add.html django thinks it's submitting the form in location_check_add again.

Edit: Adding urls.py for reference

import users.views

urlpatterns = patterns('',
    url(r'^$', users.views.index, name = 'index'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^add/', users.views.location_check_add, name = 'add'),
)

You need to redirect, not just call the view.

    if good_location(user_lat,user_lon):
        return redirect('add')

Alright! After a lot of documentation searching and googling I found out about Django's sessions feature. Documentation here:

Documentation

In the end my code ended up looking like:

def add(request):
    if request.session.get('at_work', False):
        if request.method == 'POST':
            form = StudentModelForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/')
        else:
            form = StudentModelForm()
        context_data = {'form': form}
        return render(request, 'add.html', context_data)
    else:
        return redirect(location_check_add)

def location_check_add(request):
    if request.method == 'POST' and 'lat' in request.POST:
        user_lat = request.POST.get('lat')
        user_lon = request.POST.get('lon')
        if good_location(user_lat,user_lon):
            request.session['at_work'] = True
            return redirect(add)
        else:
            return render(request, 'location.html')
    return render (request, 'checking.html')

Basically I just passed around the session variable "at_work" to make sure the location was correct before rendering the page in the add() view. Then I was able to use the regular redirect function since I could have a url directed at add() in urls.py without people getting around the location check.

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