Question

I'm using the Django Framework on Google App Engine. I have multiple forms on the same view, to submit to different URL. Trouble is after I get a form submitted: even if the called method update the datastore and some data, the previous page (where the forms are put in) is not refreshed, showing the updated data.

I could solve this problem using jQuery or some javascrip framework, appending dinamically content returned by the server but, how to avoid it? Suggestions? Am I wrong somewhere?

A part of "secure.html" template

<form action="/addMatch" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>


Matches:
<br />
{% for m in matches%}
    {{m.description}} ---> {{m.reward}}
{% endfor%}

the "/addMatch" URL view:

def addMatch(request):
    form = MatchForm(request.POST)
    if form.is_valid():
        user = User.all().filter('facebookId =', int(request.session["pbusr"]))
        m = Match(user=user.get(),description =form.cleaned_data["description"],reward=form.cleaned_data["reward"])
        m.save()
        return HttpResponseRedirect("/secure/")
    else:
        logging.info("Not valid")
        return HttpResponseRedirect("/secure")

The view method whose seems not working:

@auth_check_is_admin
def secure(request): 
    model={}
    user = User.all().filter('facebookId =', int(request.session["pbusr"]))
    u = user.get()
    if (u.facebookFanPageId is not None and not u.facebookFanPageId == ""):
        model["fanPageName"] = u.facebookFanPageName
        model["form"] = MatchForm()
        model["matches"] = u.matches
    else:
            ....
        return render(request,"secure.html",model)

Francesco

Was it helpful?

Solution

Based on what you posted, it seems like you're redirecting properly and are having database consistency issues. One way to test this would be to look at the network tab in the Google Chrome developer tools:

  • Click on the menu icon in the upper right
  • Click on "Tools"
  • Click on "Developer Tools"
  • Click on "Network" in the thing that opened up at the bottom of the screen.

Now, there will be a new entry in the network tab for every request that your browser sends and every response it receives. If you click on a request, you can see the data that was sent and received. If you need to see requests across different pages, you might want to check the "Preserve log" box.

With the network tab open, go to your page and submit the form. By looking at the network tab, you should be able to tell whether or not your browser issued a new GET request to the same URL. If there is a new request for the same page but that request has the old content, then you have a datastore consistency issue. If there was NOT a new request that yielded a response with the data for the page, then you have a redirect issue.

If it turns out that you have a datastore consistency issue, then what's happening is the data is being stored, but the next request for that data might still get the old data. To make sure that doesn't happen, you need what's called "strong consistency."

In a normal App Engine project, you get strong consistency by putting entities in the same entity-group and using ancestor queries. I'm not certain of what database/datastore you're using for Django and how the different database layers interact with App Engine's consistency, so this could be wrong, but if you can give your users the right key and then fetch them from that key directly (rather than getting all users and filtering them by key), you might get strong consistency.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top