我创建了一个允许用户创建工作列表的表单,我已经在模型中声明了所有字段,并创建了一个模型表单和一个视图。通过管理面板向模型添加内容工作正常,表单在网站上完美显示。按提交按钮也不会抛出任何错误,但它不会保存数据。感谢任何帮助,谢谢!
模型 -
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'}),
}
网址 -
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 :(得分: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>
这样您就可以看到表单无效的原因。