Advertisement
horozov86

request.FILES

Mar 26th, 2024
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. from django.views import generic
  2. from .forms import PlaceCreateForm
  3. from .models import Place
  4.  
  5. class PlaceCreateView(generic.CreateView):
  6.     form_class = PlaceCreateForm
  7.     model = Place
  8.     template_name = 'destination/place_form.html'
  9.     success_url = reverse_lazy('travelogue_view')
  10.  
  11.     def form_valid(self, form):
  12.         place = form.save(commit=False)
  13.         place.user = self.request.user
  14.         # Check if a file was uploaded
  15.         if 'photo' in self.request.FILES:
  16.             place.photo = self.request.FILES['photo']
  17.         place.save()
  18.         return super().form_valid(form)
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement