Home > Back-end >  Django Models - Form saves but model is blank
Django Models - Form saves but model is blank

Time:08-22

Not sure my title is fully representing my problem.

I thought I would put a screenshot of the problem (admin panel), so it's clearer for everyoneDjango - Empty models

It looks like the form is savings, but nothing goes inside.

Here is the models code:

class Venue(models.Model):
    name = models.CharField(verbose_name="Name",max_length=100, null=True, blank=True)
    address = models.CharField(verbose_name="Address",max_length=100, null=True, blank=True)
    town = models.CharField(verbose_name="Town",max_length=100, null=True, blank=True)
    county = models.CharField(verbose_name="County",max_length=100, null=True, blank=True)
    post_code = models.CharField(verbose_name="Post Code",max_length=8, null=True, blank=True)
    country = models.CharField(verbose_name="Country",max_length=100, null=True, blank=True)
    longitude = models.CharField(verbose_name="Longitude",max_length=50, null=True, blank=True)
    latitude = models.CharField(verbose_name="Latitude",max_length=50, null=True, blank=True)
    city = models.CharField(max_length=120)
    
    def __str__(self):
        return str(self.name) if self.name else ''

Obviously, I am aware I have asked to return '' if self.name wasnt there. The reason why I did it, is because initially, the models was visible on the admin panel under "-" but was throwing an error message when clicking on it.

Considering I am working with a form, here is the form code:

class VenueForm(forms.ModelForm):
    name = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
    address = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
    town = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
    county = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
    post_code = forms.CharField(max_length=8, required=True, widget = forms.HiddenInput())
    country = forms.CharField(max_length=40, required=True, widget = forms.HiddenInput())
    longitude = forms.CharField(max_length=50, required=True, widget = forms.HiddenInput())
    latitude = forms.CharField(max_length=50, required=True, widget = forms.HiddenInput())
    phone = forms.CharField(max_length=120)
    web = forms.URLField(max_length=120)
    email_address = forms.CharField(max_length=120)

    class Meta:
        model = Venue
        fields = ['name', 'address', 'town', 'county', 'post_code','country','post_code','latitude','city', 'web', 'phone', 'email_address']

the views

def add_venue(request):
    submitted = False
    form = VenueForm()
    if is_ajax(request) and request.method =="POST":
        form = VenueForm(data = request.POST)
        if form.is_valid():
            form.save()
            messages.success(request,("Success!"))
            return HttpResponseRedirect('/add_venue?submitted=True')
    context = {
    'form' : form,
    'submitted' : submitted,
    'google_api_key' : settings.GOOGLE_API_KEY,
    'base_country' : settings.BASE_COUNTRY,
    }
    return render(request,"main/add_venue.html",context)

and finally the html file

<div >


<input type="text" placeholder="*Begin typing address" id="id-google-address" name="google_address">

  <form id="venueform" method="POST" action="{% url 'add_venue'%}">
    {% csrf_token %}

   <label for="name"  hidden>Name</label>
   {{form.name}}

   <label for="address"  hidden>Address</label>
   {{form.address}}

   <label for="town"  hidden>Town/City</label>
   {{form.town}}

   <label for="county"  hidden>County</label>
   {{form.county}}

   <label for="post_code"  hidden>Postal Code</label>
   {{form.post_code}}

   <label for="country"  hidden>Country</label>
   {{form.country}}

   <label for="longitude"  hidden>Longitude</label>
   {{form.longitude}}

   <label for="latitude"   hidden>Latitude</label>
   {{form.latitude}}
   <h4>Phone</h4>
   <label for="phone"   hidden>Phone</label>
   {{form.phone}}
   <h4>WebSite</h4>
   <label for="web"   hidden>Website</label>
   {{form.web}}
   <h4>Email Address</h4>
   <label for="email_address"   hidden>Email Address</label>
   {{form.email_address}}

        <button type = "submit" >Add Venue</button>
</form>
</div>

{% endblock %}

{% block extend_footer %}

<script type="text/javascript">

  var google_api_key = "{{google_api_key|safe}}";
  var base_country = "{{base_country|safe}}";

</script>

<script src="{% static 'google_places.js' %}"></script>


{% endblock %}

CodePudding user response:

def add_venue(request):
    submitted = False
    form = VenueForm()
    venue = Venue.objects.create() # <- This line is the issue.

You are creating an empty venue, and not going anything with the variable.

Then, you are saving the form

        data = form.save(commit = False)
        data.name = form.cleaned_data['name']
        data.address = form.cleaned_data['address']
        data.town = form.cleaned_data['town']
        data.county = form.cleaned_data['county']
        data.post_code = form.cleaned_data['post_code']
        data.country = form.cleaned_data['country']
        data.longitude = form.cleaned_data['longitude']
        data.latitude = form.cleaned_data['latitude']
        data.city = form.cleaned_data['city']
        data.phone = form.cleaned_data['phone']
        data.web = form.cleaned_data['web']
        data.email_address = form.cleaned_data['email_address']
        data.save()

Without having validated the form first. You need to call form.is_valid() before any of this.

Then, you are validating the form, and saving the form again. You are trying a single object, 3 times. 2 of which are executed wrong.

Then, you are not instantiating your form in the

         else:
            form = VenueForm
            if 'submitted' in request.GET:
                submitted = True

And on top of that, you should return the form POSTed, so you can send back the form.errors().

        else:
            form = VenueForm()
            if 'submitted' in request.GET:
                submitted = True

If this is a ModelForm, all that is needed is:

def add_venue(request):
    submitted = False
    form = VenueForm()
    if is_ajax(request) and request.method =="POST":
        form = VenueForm(data = request.POST)
        if form.is_valid():
            form.save()
            messages.success(request,("Success!"))
            return HttpResponseRedirect('/add_venue?submitted=True')
    context = {
    'form' : form,
    'submitted' : submitted,
    'google_api_key' : settings.GOOGLE_API_KEY,
    'base_country' : settings.BASE_COUNTRY,
    }
    return render(request,"main/add_venue.html",context)
  • Related