コード例 #1
0
ファイル: views.py プロジェクト: GlebZhnkvch/E-car-store
def create(request):
    page = request.GET.get("page", 1)
    ads = Ad.objects.filter().order_by("-created_at")
    p = Paginator(ads, settings.ITEMS_PER_PAGE)
    if request.method == 'GET':
        ads = Ad.objects.all()
        form = AdForm()
        return render(request,
                      'create.html',
                      context={
                          "form": form,
                          'page': p.page(page),
                      })
    elif request.method == 'POST':
        form = AdForm(request.POST, request.FILES)
        if form.is_valid():
            ad = form.save(commit=False)
            ad.author = request.user
            form.save()
            return render(request,
                          'create_success.html',
                          context={
                              'page': p.page(page),
                          })
        else:
            return render(request,
                          'create.html',
                          context={
                              "form": form,
                              'page': p.page(page),
                          })
コード例 #2
0
ファイル: views.py プロジェクト: drdilyor/mysite
    def post(self, request, pk: int):
        ad = get_object_or_404(Ad, pk=pk, owner=request.user)
        form = AdForm(request.POST, request.FILES or None, instance=ad)
        
        if not form.is_valid():
            context = {'form': form}
            return render(request, self.template_name, context)

        ad = form.save(commit=False)
        ad.save()
        return redirect(self.success_url)
コード例 #3
0
ファイル: views.py プロジェクト: drdilyor/mysite
    def post(self, request, pk: int = None):
        form = AdForm(request.POST, request.FILES or None)

        if not form.is_valid():
            context = {'form': form}
            return render(request, self.template_name, context)

        # Add owner to the model before saving
        ad = form.save(commit=False)
        ad.owner = request.user
        ad.save()
        return redirect(self.success_url)
コード例 #4
0
def edit_ad(request, pk):
    current_ad = Ads.objects.get(pk=pk)
    if request.user == current_ad.author:
        if request.method == "POST":
            form = AdForm(request.POST, request.FILES, instance=current_ad)

            if form.is_valid():
                form.save()
                for f in request.FILES.getlist('photos'):
                    data = f.read()
                    photo = Images(ad=current_ad)
                    photo.img.save(f.name, ContentFile(data))
                    photo.save()
                messages.success(request, 'Ваше объявление успешно обновлено')
                return redirect('edit_ad', pk=pk)
        else:
            form = AdForm(instance=current_ad)
    else:
        return redirect('home')

    data = {'form': form, 'current_ad': current_ad}

    return render(request, 'ads/edit_ad.html', data)
コード例 #5
0
ファイル: views.py プロジェクト: kasappeal/Django-May-2018
 def post(self, request):
     """
     Procesa el formulario para crear un anuncio
     :param request: objeto HttpRequest
     :return: HttpResponse con la respuesta
     """
     ad = Ad()
     ad.owner = request.user
     form = AdForm(request.POST, request.FILES, instance=ad)
     if form.is_valid():
         # creamos el anuncio
         ad = form.save()
         # limpiar el formulario
         form = AdForm()
         # Devolvemos un mensaje de OK
         messages.success(request, 'Anuncio creado correctamente')
     context = {'form': form}
     return render(request, 'ads/form.html', context)
コード例 #6
0
 def post(self, request):
     ad_form = AdForm(request.POST)
     formset = formset_factory(ImageForm, max_num=5)
     image_form_set = formset(request.POST, request.FILES)
     if ad_form.is_valid() and image_form_set.is_valid():
         new_ad = ad_form.save(commit=False)
         new_ad.save()
         for form in image_form_set.forms:
             if form.cleaned_data:
                 image = form.save(commit=False)
                 image.ad = new_ad
                 image.save()
         messages.success(request, 'Your ad successfully saved!')
         return redirect('/advert/%s/' % new_ad.id)
     else:
         return render(request, 'addition.html', {
             'ad_form': ad_form,
             'image_form_set': image_form_set
         })
コード例 #7
0
ファイル: test_forms.py プロジェクト: slavapavlutin/bulletin
 def test_updating_published_at(self):
     "Published_at is not updated after creating."
     published_at = self.ad.published_at
     form = AdForm(self.data, instance=self.ad)
     instance = form.save()
     self.assertEquals(instance.published_at, published_at)