Django request.session does not resolve

喜你入骨 提交于 2019-12-13 06:17:07

问题


I have a ManyToMany relation between 2 of my models in the same app. This looks like following:

class Event(models.Model):
    eventID = models.CharField(deafult = random_eventID)
    signal = models.ManyToManyField(Signal)
    ....
    ....

Now, in my Form (using ModelForm) my eventID field is already populated with the eventID every time i refresh the page (because it gets a new random_eventID every time i refresh the page). This way when, in my forms i select to add a new signal (I want to be able to add signals when i create an event)...it goes to a different view. I save the event and when i return back to the Event page, the eventID is changed again. I want to have all the data which the user has already filled/selected in the form to be present when it returns back to the Event page after adding lots of different stuff.

Solutions i thought of :

1 - I cannot make changes to my model so as to include another column and save the Event before going to another page & later retrieve it back.

2 - Using sessions i save all the data already present in all the fields an later retrieve it back..( This way HTTP is no more stateless, but it serves my purpose.)

3 - Will Ajax help in doing any update (which i don't understand it will)

I tried it using session and came cross this silly error, which i am not able to resolve.

views.py

def create(request):
    if request.POST:
        form = EventForm(request.POST)
        form .save()
        del request.session['event_id']
        return HttpResponseRedirect('/Event')
    else:
        event_session = request.session.get('event_id')
        if event_session is not None:
            form = EiEventForm(initial={'eventID' : event_session}
        else:
            form = EventForm()
            request.session['event_id'] = form('eventID').value()
    args = {}
    args.update(csrf(request))
    args['form'] = form
    return render_to_response('event.html', args)

With the above, after debugging i do not get the current value in the eventID field..I tried some other ways as well but with no success.

The request.GET.get('eventID') returns None..How can i get the values from my field ?

Also, is there a better way to accomplish the desired result except sessions.

Any help would be great help!

来源:https://stackoverflow.com/questions/19240316/django-request-session-does-not-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!