django - name 'category_id' is not defined -


i trying id of category error keeps poping. here models.py

class category(models.model):     user = models.foreignkey(settings.auth_user_model, related_name='category_created')     name = models.charfield(max_length=500, db_index=true)     slug = models.charfield(max_length=500, blank=true)     images = models.imagefield(upload_to='users/%y/%m/%d', blank=true)      def __str__(self):         return self.name      def save(self, *args, **kwargs):         if not self.slug:             self.slug = slugify(self.name)             super(category, self).save(*args, **kwargs)  class product(models.model):     user = models.foreignkey(settings.auth_user_model, related_name='product')     category = models.foreignkey(category, related_name='products')     name = models.charfield(max_length=500, db_index=true)     slug = models.charfield(max_length=500, blank=true)     images = models.imagefield(upload_to='users/%y/%m/%d', blank=true)     description = models.textfield(blank=true)     price = models.decimalfield(max_digits=10, decimal_places=2)     stock = models.positiveintegerfield(blank=true)     available = models.charfield(max_length=25)     created = models.datetimefield(auto_now_add=true)     updated = models.datetimefield(auto_now=true)      class meta:         ordering = ('name',)     def __str__(self):         return self.name      def save(self, *args, **kwargs):         if not self.slug:             self.slug = slugify(self.name)             super(product, self).save(*args, **kwargs) 

this views errors occuring , don't know how fix views , sure problem views.py

@login_required def product(request):     if request.method == 'post':         category = category.objects.get(pk = category_id)         product_form = productform(data=request.post, files=request.files)         if product_form.is_valid():             new_item = product_form.save(commit=false)             new_item.category = category             new_item.save()             messages.success(request, 'product created')         else:             messages.error(request, 'product failed created')     else:         product_form = productform()     return render(request, 'shop/product/product_create.html', {'product_form':product_form}) 

hi ekhorutomwen harsley,

if try create new object, don't need id because django added automatically.

you have create new category object that

    @login_required def product(request):     if request.method == 'post':         category = category()         product_form = productform(data=request.post, files=request.files)         if product_form.is_valid():             new_item = product_form.save(commit=false)             new_item.category = category             new_item.save()             messages.success(request, 'product created')         else:             messages.error(request, 'product failed created')     else:         product_form = productform()     return render(request, 'shop/product/product_create.html', {'product_form':product_form}) 

if want access category exists in database. can access in way :

category_id = 3 // id want access category = category.objects.get(id=category_id) 

i hope you


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -