def new(request): ''' Create a new listing with request.user as owner ''' if request.method == 'POST': form = ListingForm(request.POST) if form.is_valid: l = form.save(commit=False) l.owner = request.user l.save() return HttpResponseRedirect(reverse('listings_all')) else: form = ListingForm() return create_object(request, form_class=ListingForm)
def post(self, request, *args, **kwargs): form = ListingForm(request.POST) if form.is_valid(): listing = form.save(commit=False) listing.author = request.user listing = form.save() listing.save() return HttpResponseRedirect( reverse_lazy('listing-details-page', args=[listing.slug]))
def test_form(self): form_info = { 'title': "Test title", 'description': "The description in here", 'link_to_image': "www.theimage.com", 'price_per_month': 120, 'total_area': 123.23, } form = ListingForm(data=form_info) self.assertTrue(form.is_valid())