2015-07-19 239 views
1

我已經制作了一個允許用戶創建工作列表的表單,我已經在我的模型中聲明瞭所有的字段並創建了一個模型表單和一個視圖。通過管理面板將模型添加到模型中可以正常工作,並且表單在網站上完美顯示。按提交按鈕也不會引發錯誤,但不會保存數據。任何幫助表示感謝,謝謝!Django模型不會保存

型號 -

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 

形式 -

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'), 

) 

視圖 -

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 %} 
+0

任何錯誤或追溯? – kartikmaji

+0

nope,這是我提交http://puu.sh/j4szb/1c815c6e75.png –

+0

時發生的唯一情況在檢查該表單之前,如何保存該表單 – Pynchia

回答

1

你不是在你的模板顯示任何領域的錯誤。對於每個{{ form.field }},您還需要有{{ 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> 

這將允許您查看錶單無效的原因。