gpt4 book ai didi

python - 用于在数据库中创建数据的 django api

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:21 25 4
gpt4 key购买 nike

每个人,我正在尝试编写一个 django api 来在 db 中创建数据,这是我的

模型.py

class ProductsTbl(models.Model):
model_number = models.CharField(
max_length=255,
blank=True,
unique=True,
error_messages={
'unique': "這 model number 已經被註冊了 ."
}
)
name = models.CharField(max_length=255, blank=True, null=True)
material = models.CharField(max_length=255, blank=True, null=True)
color = models.CharField(max_length=255, blank=True, null=True)
feature = models.TextField(blank=True, null=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField(auto_now=True)
release = models.DateTimeField(blank=True, null=True)
twtime = models.DateTimeField(blank=True, null=True)
hktime = models.DateTimeField(blank=True, null=True)
shtime = models.DateTimeField(blank=True, null=True)
jptime = models.DateTimeField(blank=True, null=True)
suggest = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
cataloggroup = models.ManyToManyField(CatalogGroup)
place = models.ManyToManyField(Place)
scale = models.ManyToManyField(Scale)
slug = models.SlugField(unique=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
image = models.ImageField(upload_to=get_imagep_Product, blank=True)

def __unicode__(self):
return self.name

def save(self, *args, **kwargs):
''' On save, update timestamps '''

if not self.id:
self.created = timezone.now()

return super(ProductsTbl, self).save(*args, **kwargs)

views.py 我可以从表单创建数据 ok

View .py

def create_thing(request):
form_class = ProductsTblForm
# if we're coming from a submitted form, do this
if request.method == 'POST':
# grab the data from the submitted form and apply to # the form
form = form_class(request.POST, request.FILES)
if form.is_valid():
# create an instance but do not save yet
thing = form.save(commit=False)
# set the additional details
thing.user = request.user
thing.slug = slugify(thing.model_number)
# save the object
thing.save()
# redirect to our newly created thing
return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
else:
form = form_class()
return render(
request,
'things/create_thing.html',
{'form': form, 'login_user': request.user}
)

但是,当涉及到 api 时,我只是失败了,这是我的

api/urls.py

from django.conf.urls import url, include
from . import views

urlpatterns = [
.....
url(r'^productsTbls/create_thing/$',views.api_create_thing,name='api_create_t'),

]

api/views.py
......
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
........



@csrf_exempt
def api_create_thing( user_id ,model_number,name,release,feature, material,image,suggest,description,):
p1 = ProductsTbl(user_id = user_id ,model_number = model_number,slug = model_number ,name = name ,release = release ,feature = feature, material = material ,image=image,suggest = suggest,description = description )
p1.save()
return redirect('/api/productsTbls/')

这是我的错误信息

enter image description here

感谢所有回复我的人

最佳答案

api_create 方法似乎完全多余,可以安全删除。你的 urls.py 应该指向 views.py 中的 create_thing 方法

urlpatterns = [
.....
url(r'^productsTbls/create_thing/$',views.create_thing,name='api_create_t'),

]

不需要 api_create_thing 的原因是因为您已经拥有使用表单从用户那里收集数据并将其保存到数据库的代码。第二点是使用GET 方法接受用户输入不是一个好主意。您应该始终尝试使用 POST(create_thing)已经这样做了。

关于python - 用于在数据库中创建数据的 django api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com