Re: django form not saving items to database

2019-07-08 Thread KIKOYO ISAAC
Some lessons about templates with django


On Mon, Jul 1, 2019 at 8:29 AM Joe Reitman  wrote:

> Try this:
>
>
> def sell(request):
>  if request.method == 'POST':
>   form = ProductsForm(request.POST)
>   if form.is_valid():
>   print('form is valid')
>   form.save()
>   return redirect('home')
>
>  else:
>  form = ProductsForm()
>  return render(request, 'sell/products_form.html', {'form': form})
>
>
> On Monday, July 1, 2019 at 6:35:31 AM UTC-5, brian wrote:
>>
>> model
>>
>> class Products(models.Model):
>>
>> UNIT = (
>> ('whole', 'whole unit'),
>> ('item', 'per single item'),
>> )
>>
>>
>> # category = models.ForeignKey(Category, related_name='products', 
>> on_delete=models.CASCADE)
>> ProductName = models.CharField(max_length=255)
>> user = models.ForeignKey(User, on_delete=models.CASCADE)
>> ProductDescription = models.TextField(max_length=500, blank=True)
>> price = models.FloatField()
>> location = models.CharField(choices = COUNTIES, max_length=300)
>> # category = models.CharField( choices = CATEGORIES, max_length=10, 
>> default='other')
>> category = models.ForeignKey(Category, related_name='products', 
>> on_delete=models.CASCADE)
>>
>> unitofsale = models.CharField(max_length=10, choices=UNIT)
>> image = models.FileField(upload_to='products_images/', blank=True)
>> sublocation = models.CharField(max_length=100)
>> created= models.DateTimeField(auto_now_add=True)
>> # slug = models.SlugField(max_length=200,db_index=True)
>>
>> class Meta:
>> ordering = ('-created',)
>> # index_together = (('id', 'slug'),)
>>
>> view
>>
>> def sell(request):
>>  if request.method == 'POST':
>>   form = ProductsForm()
>>   form = ProductsForm(request.POST, request.FILES, instance = 
>> request.user)
>>   if form.is_valid():
>>   print('form is valid')
>>   form = form.save(commit=True)
>>   user = request.user
>>   form.user = user
>>   form.save()
>>   return redirect('home')
>>
>>  else:
>>  form = ProductsForm()
>>  return render(request, 'sell/products_form.html', {'form': form})
>>
>> template
>>
>> > enctype="multipart/form-data">
>> {% csrf_token %}
>> 
>> 
>> 
>> Product Name
>> {{  form.ProductName}}
>>
>> 
>>
>>
>> 
>> 
>> 
>> Product Price
>> {{ form.price }}
>> 
>> 
>> 
>> 
>> 
>> 
>> County
>> {{  form.location}}
>>
>> 
>>
>>
>> 
>> 
>> 
>> Sub-location
>> {{ form.sublocation }}
>> 
>> 
>> 
>> 
>> 
>> 
>> Category
>> {{  form.category}}
>>
>> 
>>
>>
>> 
>> 
>> 
>> Unit of Sale
>> {{ form.unitofsale }}
>> 
>> 
>> 
>> 
>> 
>> 
>> Product Description
>> {{  form.ProductDescription}}
>>
>> 
>>
>>
>> 
>> 
>> 
>> Upload Product Image
>> {{ form.image }}
>> 
>> 
>> 
>> 
>> 
>> 
>>  Sell 
>>
>> 
>>
>>
>> 
>> 
>>
>>
>> 
>>
>> {##}
>> 
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0066c21c-e4c4-43ed-9314-05ec360bbac9%40googlegroups.com
> 
> .
> For more options, visit htt

Re: django form not saving items to database

2019-07-01 Thread Joe Reitman


Try this:


def sell(request):
 if request.method == 'POST':
  form = ProductsForm(request.POST)
  if form.is_valid():
  print('form is valid')
  form.save()
  return redirect('home')

 else:
 form = ProductsForm()
 return render(request, 'sell/products_form.html', {'form': form})


On Monday, July 1, 2019 at 6:35:31 AM UTC-5, brian wrote:
>
> model
>
> class Products(models.Model):
>
> UNIT = (
> ('whole', 'whole unit'),
> ('item', 'per single item'),
> )
>
>
> # category = models.ForeignKey(Category, related_name='products', 
> on_delete=models.CASCADE)
> ProductName = models.CharField(max_length=255)
> user = models.ForeignKey(User, on_delete=models.CASCADE)
> ProductDescription = models.TextField(max_length=500, blank=True)
> price = models.FloatField()
> location = models.CharField(choices = COUNTIES, max_length=300)
> # category = models.CharField( choices = CATEGORIES, max_length=10, 
> default='other')
> category = models.ForeignKey(Category, related_name='products', 
> on_delete=models.CASCADE)
>
> unitofsale = models.CharField(max_length=10, choices=UNIT)
> image = models.FileField(upload_to='products_images/', blank=True)
> sublocation = models.CharField(max_length=100)
> created= models.DateTimeField(auto_now_add=True)
> # slug = models.SlugField(max_length=200,db_index=True)
>
> class Meta:
> ordering = ('-created',)
> # index_together = (('id', 'slug'),)
>
> view
>
> def sell(request):
>  if request.method == 'POST':
>   form = ProductsForm()
>   form = ProductsForm(request.POST, request.FILES, instance = 
> request.user)
>   if form.is_valid():
>   print('form is valid')
>   form = form.save(commit=True)
>   user = request.user
>   form.user = user
>   form.save()
>   return redirect('home')
>
>  else:
>  form = ProductsForm()
>  return render(request, 'sell/products_form.html', {'form': form})
>
> template
>
>  enctype="multipart/form-data">
> {% csrf_token %}
> 
> 
> 
> Product Name
> {{  form.ProductName}}
>
> 
>
>
> 
> 
> 
> Product Price
> {{ form.price }}
> 
> 
> 
> 
> 
> 
> County
> {{  form.location}}
>
> 
>
>
> 
> 
> 
> Sub-location
> {{ form.sublocation }}
> 
> 
> 
> 
> 
> 
> Category
> {{  form.category}}
>
> 
>
>
> 
> 
> 
> Unit of Sale
> {{ form.unitofsale }}
> 
> 
> 
> 
> 
> 
> Product Description
> {{  form.ProductDescription}}
>
> 
>
>
> 
> 
> 
> Upload Product Image
> {{ form.image }}
> 
> 
> 
> 
> 
> 
>  Sell 
>
> 
>
>
> 
> 
>
>
> 
>
> {##}
> 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0066c21c-e4c4-43ed-9314-05ec360bbac9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


django form not saving items to database

2019-07-01 Thread brian
model

class Products(models.Model):

UNIT = (
('whole', 'whole unit'),
('item', 'per single item'),
)


# category = models.ForeignKey(Category, related_name='products', 
on_delete=models.CASCADE)
ProductName = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE)
ProductDescription = models.TextField(max_length=500, blank=True)
price = models.FloatField()
location = models.CharField(choices = COUNTIES, max_length=300)
# category = models.CharField( choices = CATEGORIES, max_length=10, 
default='other')
category = models.ForeignKey(Category, related_name='products', 
on_delete=models.CASCADE)

unitofsale = models.CharField(max_length=10, choices=UNIT)
image = models.FileField(upload_to='products_images/', blank=True)
sublocation = models.CharField(max_length=100)
created= models.DateTimeField(auto_now_add=True)
# slug = models.SlugField(max_length=200,db_index=True)

class Meta:
ordering = ('-created',)
# index_together = (('id', 'slug'),)

view

def sell(request):
 if request.method == 'POST':
  form = ProductsForm()
  form = ProductsForm(request.POST, request.FILES, instance = 
request.user)
  if form.is_valid():
  print('form is valid')
  form = form.save(commit=True)
  user = request.user
  form.user = user
  form.save()
  return redirect('home')

 else:
 form = ProductsForm()
 return render(request, 'sell/products_form.html', {'form': form})

template


{% csrf_token %}



Product Name
{{  form.ProductName}}







Product Price
{{ form.price }}






County
{{  form.location}}







Sub-location
{{ form.sublocation }}






Category
{{  form.category}}







Unit of Sale
{{ form.unitofsale }}






Product Description
{{  form.ProductDescription}}







Upload Product Image
{{ form.image }}






 Sell 










{##}



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f69eafee-4247-42b9-9db9-a741d84a02ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.