简体   繁体   中英

Django modelform will not save

I have made a form which allows users to create a job listing, I have declared all the fields in my model and have created a model form and a view. Adding things to the model through the admin panel works fine and the form displays perfectly on the website. Pressing the submit button also throws no errors however it does not save the data. Any help appreciated, thanks!

Model -

class JobListing(models.Model):

    region_choice = (
        ('1', 'Auckland'),
        ('2', 'Wellington'),
        ('3', 'Christchurch')
    )
    industry_choice = (
        ('1', 'Accounting'),
        ('2', 'Agriculture, fishing & forestry'),
        ('3', 'Automotive'),
        ('4', 'Banking, finance & insurance'),
        ('5', 'Construction & Architecture'),
        ('6', 'Customer service'),
    )
    employment_type_choice = (
        ('1', 'Full Time'),
        ('2', 'Part Time'),
        ('3', 'One-off'),
        ('4', 'Other')
    )

    user = models.OneToOneField(User, unique=True)
    business_name = models.CharField(max_length=50)
    pay_rate = models.FloatField()
    employment_type = models.CharField(max_length=10, choices=employment_type_choice)
    job_description = models.CharField(max_length=2000)
    business_address_region = models.CharField(max_length=50, choices=region_choice)
    business_address_suburb = models.CharField(max_length=50)
    business_industry = models.CharField(max_length=50, choices=industry_choice)
    job_id = models.AutoField("ID", primary_key=True, editable=False, unique=True)

    class Meta:
        verbose_name = 'Job Listing'

    def __unicode__(self):
        return "%s" % self.business_name

Forms -

class JobListingForm(forms.ModelForm):

    class Meta:
        model = JobListing
        fields = ['business_name', 'pay_rate', 'employment_type', 'job_description', 'business_address_region',
            'business_address_suburb', 'business_industry']
        widgets = {
            'business_name': forms.TextInput(attrs={'class': 'form-input', 'required': 'true', 'placeholder': 'Name of Business'}),
            'pay_rate': forms.NumberInput(attrs={'class': 'form-input', 'required': 'true', 'placeholder': 'Hourly Rate or One Off Amount'}),
            'employment_type': forms.Select(attrs={'class': 'form-input', 'required': 'true'}),
            'job_description': forms.Textarea(attrs={'class': 'form-textarea', 'required': 'true',
                'placeholder': 'Tell us additional information about your job listing e.g. Times, Business Info, Number of positions etc. (2000 Character Limit)'}),
            'business_address_region': forms.Select(attrs={'class': 'form-input', 'required': 'true'}),
            'business_address_suburb': forms.TextInput(attrs={'class': 'form-input', 'required': 'true', 'placeholder': 'Business Suburb'}),
            'business_industry': forms.Select(attrs={'class': 'form-input', 'required': 'true'}),
        }

URLS -

from django.conf.urls import patterns, url
from profiles import views

urlpatterns = patterns('',
    url(r'^createjoblisting/', views.createjoblisting, name='createjoblisting'),

)

Views -

from django.shortcuts import render
from forms import JobListingForm
from models import JobListing


def createjoblisting(request):

    f = JobListingForm(request.POST)

    if f.is_valid():
        profile = f.save(commit=False)
        profile.user = request.user
        profile.save()

    context = {
        "form": f
    }

    return render(request, "createjoblisting.html", context)

createjoblisting.html -

{% extends "base.html" %}

{% block content %}
<div id="createjoblisting">
    <h1 class="pageheader">Create a Job Listing</h1>
    <form class="createjoblisting" id="createjoblisting_form" method="post" action="{% url 'createjoblisting' %}">
        {% csrf_token %}
        {{ form.non_field_errors }}
        <p> <label for="id_username" class="form-input-label">Business Name</label><br>
        {{ form.business_name }}<br><p>
        <p><label for="id_username" class="form-input-label">Pay Rate</label><br>
        {{ form.pay_rate }}<br></p>
        <p><label for="id_username" class="form-input-label">Employment Type</label><br>
        {{ form.employment_type }}<br><p>
        <p><label for="id_username" class="form-input-label">Job Description</label><br>
        {{ form.job_description }}<br><p>
        <p><label for="id_username" class="form-input-label">Business Region</label><br>
        {{ form.business_address_region }}<br><p>
        <p><label for="id_username" class="form-input-label">Business Suburb</label><br>
        {{ form.business_address_suburb }}<br><p>
        <p><label for="id_username" class="form-input-label">Business Industry</label><br>
        {{ form.business_industry }}<br><p>
        <button type="submit" class="form-button">Create Job Listing</button>
    </form>
</div>
{% endblock %}

You're not displaying any of the field errors in your template. For each of {{ form.field }} , you also need to have {{ form.field.errors }} .

<p> <label for="id_username" class="form-input-label">Business Name</label><br>
    {{ form.business_name }}
    {{ form.business_name.errors }}
<br><p>

That will allow you to see the reason that the form is invalid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM