Edit Listing Form is Duplication

I have a listing system on my website and am currently creating a page which allows the user to edit their listing, I've got most of it working however I am stuck on saving their updated form. Currently if they save the edited form rather than updating the model it creates a whole new listing entry and still leaves the old one there. If anyone could please look at my code that would be great, I've been stuck on this problem for so long!

View -

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

Model -

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

Piece of code from my registration app which could be affecting it?

def signup(self, request, user):
    SignUpProfile.objects.create(
        user=user,
        account_type=self.cleaned_data['account_type'],
        contact_number=self.cleaned_data['contact_number']
    )

Form -

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

Form 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>

URLS -

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

Answers


Your form's action is wrong. Change it to:

<form action="{% url 'editlisting' %}" class="createjoblisting" id="createjoblisting_form" method="post" >

As you have copied the creation template, your form posts your data to the view which creates the listing instead of editing it.


Need Your Help

Getting an irregular-shaped object's pixel area in Photoshop

image photoshop photo photos photoshop-script

Is there a way to get an oddly-shaped object's pixel area in CS5? Eg. Circle/ellipse. Or, if there's some easy-to-automate way to do it outside of Photoshop.

QuickBlox downloading content receiving QBASIHTTPRequestErrorDomain code 1

ios objective-c quickblox

I'm using the iOS SDK to access download content from the content module. It seems in the delegate method -(void)completedWithResult:(Result*)result, I can have cases where result.success is YES wh...