django api用于在db中创建数据

时间:2016-07-25 10:47:03

标签: python django api django-models django-rest-framework

每一个,我都想写一个django api在db中创建数据,这里是我的

models.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

创建数据

views.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 ,,,我jsut失败,这是我的

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

感谢任何回复我的人

1 个答案:

答案 0 :(得分:2)

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

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

]

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