Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##views.py
- def pg_session_addedcost(request, pk = 1):
- user = request.user
- if not user.is_authenticated:
- return redirect('autherror')
- curSession = Session.objects.get(id = pk)
- if request.method == "POST":
- print (request.POST)
- newAddedCostForm = AddedCostForm(request.POST, request.FILES)
- if newAddedCostForm.is_valid():
- newAddedCost = AddedCost(
- comment=newAddedCostForm.cleaned_data['comment'],
- session=curSession,
- image=newAddedCostForm.cleaned_data['image'],
- cost=newAddedCostForm.cleaned_data['cost'],
- )
- newAddedCost.save()
- else:
- print (AddedCostForm.errors)
- ##forms.py
- class AddedCostForm(ModelForm):
- class Meta:
- model = AddedCost
- fields = ['image', 'comment', 'cost']
- ##models.py
- class AddedCost(models.Model):
- session = models.ForeignKey(
- Session,
- verbose_name= 'Смена',
- on_delete = models.CASCADE,
- default=0,
- blank=True,
- )
- image = models.ImageField(
- 'Сопроводительное изображение',
- upload_to = 'images/forSession',
- )
- comment = models.TextField(
- 'Комментарий',
- blank=True,
- )
- cost = models.FloatField(
- 'Дополнительные расходы',
- default = 0,
- )
- ##HTML
- {% load widget_tweaks %}
- <form method="POST">
- {% csrf_token %}
- {% render_field form.image %}<br>
- {% render_field form.cost %}<br>
- {% render_field form.comment %}<br>
- <button type="submit">
- Добавить
- </button>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement