Django编辑列表表单未更新

时间:2015-10-07 09:33:08

标签: django django-models django-forms

我的网站上有一个列表系统,我正在创建一个页面,允许用户编辑他们的列表,我已经完成了大部分工作,但我仍然坚持保存更新的表单。目前,如果他们保存已编辑的表单而不是更新模型,则会创建一个全新的列表条目,并且仍然保留旧的列表条目。如果有人可以请看我的代码那将是伟大的。

查看 -

@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)

        if form.is_valid():
            profile = form.save(commit=False)
            profile.user = request.user
            profile.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 = (
        ('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.CharField(max_length=50)
    job_title = models.CharField(max_length=30)
    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)
    contact_method = models.CharField(max_length=50)
    active_listing = models.BooleanField(default=True)

    class Meta:
        verbose_name = 'Job Listing'

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

我的注册应用程序中可能会影响它的一段代码?

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', 'business_name', 'pay_rate', 'employment_type', 'job_description', 'business_address_region',
            'business_address_suburb', 'business_industry', 'contact_method']
        widgets = {
            'job_title': forms.TextInput(attrs={'class': 'form-input', 'required': 'true', 'placeholder': 'Job Title'}),
            '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'}),
            'contact_method': forms.TextInput(attrs={'class': 'form-input', 'required': 'true', 'placeholder': 'Email or Contact Number'}),
        }

0 个答案:

没有答案