python - AttributeError type object Person has no attribute id -
i created edit form edit name of person. based on related stackoverflow answers on subject changed model , view, after changes follow error persists:
attributeerror @ /persion/4/edit type object 'person' has no attribute 'id'
models.py:
class person(models.model): person_text = models.charfield(max_length=200) pub_date = models.datetimefield(auto_now_add=true)
urls.py
url(r'^(?p<pk>\d+)/edit/$', views.person_edit, name='person_edit'),
views.py
def person_edit(request, pk): obj = get_object_or_404(person, pk=pk) if request.method == "post": form = personform(request.post, instance=obj) if form.is_valid(): obj = form.save(commit=false) obj.name = request.person_text obj.save() return redirect('/person/index.html',context) else: form = personform(instance=obj) return render(request, 'person/edit_person.html', {'form': form})
edit_person.html
{% block content %} <form action ="" method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">update</button> </form> {%endblock content %}
update changed views.py to:
def person_edit(request, pk): person = get_object_or_404(person, pk=pk) if request.method == "post": form = personform(request.post, instance=person) if form.is_valid(): person = form.save(commit=false) person.name = request.post['person_text'] person.save() return redirect('/person/index.html',context) else: form = personform(instance=person) return render(request, 'person/edit_person.html', {'form': form})
in else clause, passing class - question, presume code has person - instead of instance. should pass obj
in other clause.
(next time, please post actual code, , full traceback.)
Comments
Post a Comment