我需要帮助识别当前代码中的错误。几点注意事项:
我的问题是能够从客户端收集数据并将其发送到api。我的request.POST对象返回空,即使应用程序的其余部分似乎正在工作。我希望React表单中的数据能够POST到Django服务器。
我尝试过的事情:
views.py
from __future__ import unicode_literals
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.views.generic import TemplateView
from api.models import Risk
class create_user(TemplateView):
def index(self, request):
template = 'index.html'
return render(request, self.template)
def post(self, request):
if request.method == 'POST':
risk_type = request.POST.get('risk_type')
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
age = request.POST.get('age')
zipCode = request.POST.get('zipCode')
prize_amount = request.POST.get('prize_amount')
currency = request.POST.get('currency')
Risk.objects.create(
risk_type = risk_type,
first_name = first_name,
last_name = last_name,
age = age,
zipCode = zipCode,
prize_amount = prize_amount,
currency = currency
)
return redirect('/')
print('*'*50)
print(request.POST)
print('*'*50)
return HttpResponse("<h1>Submitted</h1>")
models.py
from __future__ import unicode_literals
import datetime
from django.utils import timezone
from django.db import models
class Risk(models.Model):
risk_type = models.CharField(max_length=32, blank=True, null=True)
first_name = models.CharField(max_length=32, blank=True, null=True)
last_name = models.CharField(max_length=32, blank=True, null=True)
age = models.IntegerField(default=0, blank=True, null=True)
zipCode = models.IntegerField(default=0, blank=True, null=True)
prize_amount = models.IntegerField(default=0, blank=True, null=True)
created_date = models.DateTimeField(default=timezone.now, blank=True, null=True)
currency = models.CharField(max_length=32, blank=True, null=True)
urls.py ==&gt; /djangorest/urls.py
from django.conf.urls import url, include
from api import views
from django.contrib import admin
from django.views import generic
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include('api.urls')),
url(r'^$',
generic.TemplateView.as_view(template_name='index.html'), name="home")
]
urls.py ==&gt; /api/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.create_user.as_view(), name='create-user'),
]
index.html ==&gt; templates / index.html
{% load render_bundle from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div id="container"></div>
{% render_bundle 'main' %}
{% csrf_token %}
</body>
</html>
答案 0 :(得分:0)
这里有一些注释希望对您有所帮助:
django-form为您的输入字段创建了一些HTML和CSS,但在reactjs中, 我们编写JSX来创建类似于HTML的输入字段,您可以阅读here。
在React中,我们与服务器的交互是通过ajax实现的。您只需将ajax请求发送到服务器并获得响应而无需刷新页面,并且此交互是与JSON format进行的。在后端,您也必须返回JSON。无论使用Django辅助方法还是django-rest-framework之类的第三方库。
请确保您正在向服务器发送POST请求,因为您认为该请求有条件。
如果这些注释没有写明,请使用
在视图中显示您收到的请求print(request)
ps:您可以使用postman快速测试视图。