gpt4 book ai didi

python - Django 模型不会保存

转载 作者:太空宇宙 更新时间:2023-11-03 17:33:52 25 4
gpt4 key购买 nike

我制作了一个允许用户创建职位列表的表单,我已经声明了模型中的所有字段并创建了模型表单和 View 。通过管理面板向模型添加内容效果很好,并且表单在网站上完美显示。按提交按钮也不会引发错误,但不会保存数据。感谢任何帮助,谢谢!

型号 -

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

最佳答案

您没有在模板中显示任何字段错误。对于每个 {{ 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>

这将使您能够看到表单无效的原因。

关于python - Django 模型不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com