Mist  Aurora
Mist Aurora

Reputation: 23

Django store to db using POST without django form

I'm new in Django. Besides using Django form to access django database, I'm now trying to use POST request to do that. But it's doesn't work, there is no data store to database.

In views.py

def event_join(request):
    print ("event_join")
    if request.method == 'POST':
        userID=request.GET['usrID']

        join_id = e_id
        usr = Usr.objects.get(usr_id=userID)
        if join_id in usr.join_ids.split(','):
         print "Joined already!"
        else:
            usr.join_ids += ',' + join_id
            usr.save()

            Event_member.objects.create(
                e_id = join_id,
                usr_id = userID,
                star_to_emer = 0,
                star_from_emer = 0,
            )

    return render(request, 'index.html', {
    'unfinished_events': Event.objects.filter(finish=0)})

And button active function join

var join = function() {
  console.log(userID);
  $.post('/event_join', {usrID:userID}, function(data) {});
}

In urls.py - urlpatterns

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

Upvotes: 0

Views: 84

Answers (1)

Alex Morozov
Alex Morozov

Reputation: 5993

You're checking the GET parameters in a POST call. Modify your code at least to do:

userID = request.POST['usrID']

Next, you're posting to /event_join, but your urls.py is configured to handle the path with the trailing slash, ^event_join/$. Make them consistent.

Upvotes: 3

Related Questions