Auto populate form field - django
I am trying to auto populate a field in a form so the user does not have the input the data.
In this case, the user will start from a show_venue
page which is going to display a number of products.
By clicking on one of these products, the user is redirected to a product page where a review can be left as part of a form.
The form requries the user to select the venue where the review took place.
I am looking to simplify this.
Since the user already come from a venue_id
page, I would like to auto fill this field.
I tried different things found in this forum and left my last attempt, which currently deliver the following error message: cannot unpack non-iterable NoneType object
.
Slight quirk - you will notice the form is actually being saved in the show_venue
function and not on the show_product_from_venue
function (product page). This is due to a redirection process I had to implement: the user is redirected to the previous venue_ID page after submitting the form: made this happen with a javascript in the template. I am not adding the template page to keep the question concise and dont think this will be important to my question.
views
def show_venue(request, venue_id):
if request.method == "POST" and 'btnreview_form' in request.POST:
form = CatalogueReviewForm(request.POST)
data = form.save(commit=False)
data.product_id = venue_id
data.catalogue_id = venue_id
data.venue_id = CatalogueReviewRating.objects.get(form.cleaned_data['venue']) #<-- this is where I am struggling
data.user_id = request.user.id
data.save()
ven_id = request.POST.get('venue_id')
if form.is_valid():
print(data)
form.save()
return HttpResponseRedirect(ven_id)
else:
venue = Venue.objects.get(pk=venue_id)
menu = Catalogue.objects.filter(venue=venue_id)
categories = Catalogue.objects.filter(venue=venue_id).order_by('category_order')
return render(request, 'main/show_venue.html',{'venue':venue, 'menu':menu, 'categories':categories,'product':product,'venue_id':venue_id,'object':object})
def show_product_from_venue(request, catalogue_id):
url = request.META.get('HTTP_REFERER')
product = Catalogue.objects.get(pk=catalogue_id)
form = CatalogueReviewForm()
venue_form = VenueForm()
submitted = False
data = Catalogue.objects.filter(pk=catalogue_id).annotate(
avg_rating =Avg('catalogue_comments__rating'),)
if request.method == "POST" and 'btnvenue_form' in request.POST:
venue_form = VenueForm(request.POST)
if venue_form.is_valid():
venue_form.save()
return HttpResponseRedirect(url)
else:
venue_form = VenueForm
if 'submitted' in request.GET:
submitted = True
return render(request,"main/show_product_from_venue.html", {'form':form, 'submitted':submitted, 'product':product, 'venue_form':venue_form, 'data':data})
form
class CatalogueReviewForm(forms.ModelForm):
venue = forms.ModelChoiceField(queryset = Venue.objects.all(),required=False)
class Meta:
model = CatalogueReviewRating
fields = ['review', 'rating']
model
class CatalogueReviewRating(models.Model):
user = models.ForeignKey(User,blank=True, on_delete=models.SET_NULL, null=True,related_name="catalogueusercomments")
product=models.ForeignKey(Catalogue, related_name="catalogue_comments", on_delete=models.SET_NULL, null=True)
venue = models.ForeignKey(Venue, blank=True, null=True, related_name="catalogue_venues", on_delete=models.SET_NULL)
url
path('show_venue/<venue_id>', views.show_venue,name="show-venue"),
path('show_product_from_venue/<catalogue_id>', views.show_product_from_venue,name="show-product-from-venue"),