Aditya
Aditya

Reputation: 85

Trying to edit a form using django and python

I'm trying to edit the form via my edit button.

When i click update button in my form I'm getting the following error:

FieldError at /studentapp/editrow/72/
Cannot resolve keyword 'rowid' into field Choices are: address, course, id, name, pub_date, roll

My traceback shows the error originating in this line

item = get_object_or_404(Studentapp, rowid=id) this line. 

My models.py looks like this:

class Studentapp(models.Model):
    name = models.CharField(max_length=100)
    roll = models.IntegerField()
    course = models.CharField(max_length=100)
    address = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published', auto_now=True)

    def __str__(self):
        return '%s %s %s %s' % (self.name, self.roll, self.course, self.address)

    def published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

EDIT

My view:

def editrow(request, id):
    item = get_object_or_404(Studentapp, id=id)
    if request.method=="POST":
        form = EntryForm(request.POST, instance=item)
        if form.is_valid():
            post=form.save(commit=False)
            post.save()
            return HttpResponseRedirect(reverse('studentapp:index'), id)
        else:
            form=EntryForm(instance=item)
        return render(request, 'index.html',{'form':form})
    else:
        form=EntryForm(instance=item)
        return render(request, 'index.html',{'form':form})

My urls.py

url(r'^editrow/(?P<rowid>[0-9]+)/$', views.editrow, name='editrow'),

Form that i'm using to update the entry:

{% load staticfiles %}
  <form action="{% url 'studentapp:editrow' student_detail.id  %}" id="editform" method="POST">
    {% csrf_token%}

      <div class = "form-group">
    <label for = "your_name">
      Your name:
    </label>
    <input class = "form-control" id="new_name" type = "text" name="name" value="{{ student_detail.name }}" placeholder="Enter your name">
  </div>
  <div class="form-group">
    <label for = "course_name">
      Course:
    </label>
    <input id="new_course" class = 'form-control' type = "text" name="course" value="{{ student_detail.course }}" placeholder="Enter your course">
  </div>
  <div class = "form-group">
    <label for = "rollno">
      Roll No.:
    </label>
    <input id="new_rollno" type = "text" class = 'form-control' name="roll" value="{{ student_detail.roll }}" placeholder="Enter your roll number">
  </div>
  <div class = "form-group">
    <label for ="addr">
      Address:
    </label>
    <input id="new_address" type = "text" name="address" class = 'form-control' value="{{ student_detail.address }}" placeholder="Enter your address"/>
  </div>
  <input type = "submit" value="Update" id="update" class = "btn btn-success" style="font-size:18px;" />
</form>

Upvotes: 0

Views: 46

Answers (1)

Lemayzeur
Lemayzeur

Reputation: 8525

This line is not right, your model does not have the field rowid

item = get_object_or_404(Studentapp, rowid=id) this line. # WRONG
item = get_object_or_404(Studentapp, id=id) this line. # OK

and your urls should be

url(r'^editrow/(?P<id>[0-9]+)/$', views.editrow, name='editrow'),

# url(r'^editrow/(?P<rowid>[0-9]+)/$', views.editrow, name='editrow'), # WRONG

Upvotes: 1

Related Questions