2

every one, I am trying to wrote an django api to create data in db ,here is my

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)

the views.py I can create data from form 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}
    )

however, when comes to api,,,I jsut fail, here is my

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/')

here is my error message

enter image description here

thanks for any one who reply to me

9
  • 1
    You should show your URL; you have written the view to accept 9 parameters from there, but you don't seem to be passing any. Commented Jul 25, 2016 at 10:48
  • 1
    In the meantime, since you're trying to write an API, why don't you use django-rest-framework which is intended exactly for that purpose? Commented Jul 25, 2016 at 10:48
  • right ,, I should try to use django-rest-framework,,,however I am reading it's doc right now,thank you Commented Jul 25, 2016 at 10:51
  • 2
    So i was right, your URL doesn't capture any parameters, but your view is expecting 9 parameters. Where are they supposed to be coming from? Commented Jul 25, 2016 at 11:07
  • 1
    Did you try changing your urls.py as suggested in my answer? Commented Jul 30, 2016 at 5:21

1 Answer 1

2

the api_create method seems to be totally redundant and can be deleted safely. Your urls.py should instead point to the create_thing method in views.py

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

]

The reason that api_create_thing isnt' needed is because you already have the code to gather the data from the user with a form and save it to db. The second point is that it's not a good idea to use GET method to accept user input. You should always try to use POST (which create_thing) already does.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.