我的网站上有一个列表系统,目前正在创建一个页面,允许用户编辑他们的列表,我已经完成了大部分工作,但是我仍然坚持保存更新的表单。目前,如果他们保存已编辑的表单而不是更新模型,则会创建一个全新的列表条目,并且仍然保留旧的列表条目。如果有人可以请看我的代码那么好,我已经坚持这个问题很久了!
查看 -
@login_required(redirect_field_name='login')
def editlisting(request, pk):
post = JobListing.objects.get(pk=pk)
if str(request.user) != str(post.user):
return redirect("index")
if request.method == "POST":
print("test")
form = JobListingForm(request.POST, instance=post, force_update=True)
if form.is_valid():
form.save()
return redirect('index')
else:
print("else")
form = JobListingForm(instance=post)
context = {
"form": form
}
return render(request, "editlisting.html", context)
模型 -
class JobListing(models.Model):
region_choice = (
('Auckland', 'Auckland'),
('Wellington', 'Wellington'),
('Christchurch', 'Christchurch')
)
industry_choice = (
('Accounting', 'Accounting'),
('Agriculture, fishing & forestry', 'Agriculture, fishing & forestry'),
('Automotive', 'Automotive'),
('Banking, finance & insurance', 'Banking, finance & insurance'),
('Construction & Architecture', 'Construction & Architecture'),
('Customer service', 'Customer service'),
)
employment_type_choice = (
('Full Time', 'Full Time'),
('Part Time', 'Part Time'),
('One-off', 'One-off'),
('Other', 'Other')
)
user = models.CharField(max_length=50)
job_title = models.CharField(max_length=30)
pay_rate = models.DecimalField(max_digits=10, decimal_places=2)
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)
email = models.EmailField(max_length=50, blank=True, null="True")
telephone = models.IntegerField(blank=True, null='True')
active_listing = models.BooleanField(default=True)
class Meta:
verbose_name = 'Job Listing'
def clean(self):
if not (self.email or self.telephone):
raise ValidationError("You must specify either email or telephone")
if not self.email:
self.email = "Not Provided"
def __unicode__(self):
return "%s" % self.job_title
我的注册应用程序中可能会影响它的一段代码?
def signup(self, request, user):
SignUpProfile.objects.create(
user=user,
account_type=self.cleaned_data['account_type'],
contact_number=self.cleaned_data['contact_number']
)
表格 -
class JobListingForm(forms.ModelForm):
class Meta:
model = JobListing
fields = ['job_title', 'pay_rate', 'employment_type', 'job_description', 'business_address_region',
'business_address_suburb', 'business_industry', 'telephone', 'email']
widgets = {
'job_title': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Job Title'}),
'pay_rate': forms.NumberInput(attrs={'class': 'form-input', 'placeholder': 'Hourly Rate or One Off Amount'}),
'employment_type': forms.Select(attrs={'class': 'form-input'}),
'job_description': forms.Textarea(attrs={'class': 'form-textarea',
'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'}),
'business_address_suburb': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Business Suburb'}),
'business_industry': forms.Select(attrs={'class': 'form-input'}),
'email': forms.EmailInput(attrs={'class': 'form-input', 'placeholder': 'Email'}),
'telephone': forms.NumberInput(attrs={'class': 'form-input', 'placeholder': 'Contact Numnber'}),
}
表单HTML -
<div id="createjoblisting">
<h1 class="pageheader">Edit Your 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">Job Title</label><br>
{{ form.job_title }}<br><p>{{ form.job_title.errors }}
<p><label for="id_username" class="form-input-label">Pay Rate</label><br>
{{ form.pay_rate }}<br></p>{{ form.pay_rate.errors }}
<p><label for="id_username" class="form-input-label">Employment Type</label><br>
{{ form.employment_type }}<br><p>{{ form.employment_type.errors }}
<p><label for="id_username" class="form-input-label">Job Description</label><br>
{{ form.job_description }}<br><p>{{ form.job_description.errors }}
<p><label for="id_username" class="form-input-label">Business Region</label><br>
{{ form.business_address_region }}<br><p>{{ form.business_address_region.errors }}
<p><label for="id_username" class="form-input-label">Business Suburb</label><br>
{{ form.business_address_suburb }}<br><p>{{ form.business_address_suburb.errors }}
<p><label for="id_username" class="form-input-label">Business Industry</label><br>
{{ form.business_industry }}<br><p>{{ form.business_industry.errors }}
<p><label for="id_username" class="form-input-label">Contact Number (Must provide either Contact Number or Email)</label><br>
{{ form.telephone }}<br><p>{{ form.telephone.errors }}
<p><label for="id_username" class="form-input-label">Email</label><br>
{{ form.email }}<br><p>{{ form.email.errors }}
<button type="submit" class="form-button">Update Job Listing</button>
</form>
</div>
网址 -
urlpatterns = [
url(r'^createjoblisting/', views.createjoblisting, name='createjoblisting'),
url(r'^(?P<pk>[0-9]+)/editprofile/', views.editprofile, name='editprofile'),
url(r'^(?P<pk>[0-9]+)/editlisting/', views.editlisting, name='editlisting'),
url(r'^editlistingportal/', views.editlistingportal, name='editlistingportal'),
]
答案 0 :(得分:1)
您的表单action
错误。将其更改为:
<form action="{% url 'editlisting' %}" class="createjoblisting" id="createjoblisting_form" method="post" >
在您复制了创建模板后,您的表单会将您的数据发布到创建商家信息的视图,而不是对其进行编辑。