从POST请求获取数据REST API Django

时间:2016-04-23 11:40:59

标签: django django-views

我在过去2天里一直在努力

如果我正在使用GET请求一切正常,但如果我使用POST请求,我将获得无值键

我正在使用POSTMAN来触发POST请求,我已经添加了注释,在POST请求的情况下我得到None。任何人都可以帮我解决这个问题。

我正在使用Django1.9

下面是代码

from django.shortcuts import render
from django.http.response import Http404, HttpResponse
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.models import User
from ironman.models import *
import json
from django.template import RequestContext
from dashboard.models import PromoCluster,Beacon
from datetime import datetime
from django.contrib.gis.measure import D
from django.contrib.gis.geos import *
from django.contrib.gis.geos.factory import fromstr
from django.views.decorators.csrf import csrf_exempt
from django.db import IntegrityError
from django.contrib.auth import logout

# Create your views here

@csrf_exempt
def signup(request):
    if request.method != 'POST':
        raise Http404
    email = request.POST.get('email')
    print " email is %s," % email # this is returning None.
    password = request.POST.get('password')
    print " password is %s," % password  # This is also returning None 
    name = request.POST.get('name')
    os = request.POST.get('os')
    device_id = request.POST.get('device_id')
    version = request.POST.get('version')
    device = request.POST.get('device')
    emailSecond = email;
    print " email is " , emailSecond
    try:
        user = User.objects.create_user(username=emailSecond,email=email,password=password)
        app_user_detail = AppUserDetail.objects.get(user=user)
        app_user_detail.device_id = device_id
        app_user_detail.os = os
        app_user_detail.version = version
        app_user_detail.device = device
        app_user_detail.save()
        return HttpResponse(json.dumps({"status": 200}), content_type='application/json')
    except IntegrityError:
        return HttpResponse(json.dumps({"status":201,"message":"user already exists , please try different user name "}), content_type='application/json')

enter image description here

网址。吡啶

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='landing_page_new.html'), name='home'),           
    url(r'^admin/', admin.site.urls),
    url(r'^login/', home, name='home'),
    url(r'^v1/', include(xyz_urls)),
]

xyzurls

from django.conf.urls import url,include
from django.contrib.auth.urls import urlpatterns
from ironman.views import *


urlpatterns = [
               url(r'^login/$', login),
               url(r'^signup/$', signup),

               ]

1 个答案:

答案 0 :(得分:0)

好的,问题在于PostMan。

您实际上是在提出请求。点击" x"

删除电子邮件和密码

接下来,转到Body - > formData - >在这里输入您的参数

这样可行。

只是一个注释,POST请求在Url中没有参数。