S
S
sazhyk2016-12-08 15:06:49
Django
sazhyk, 2016-12-08 15:06:49

Where is the error in Django form data edit function?

I'm trying to edit the data that is saved through the ModelForm.
modelka

class Person(models.Model):
    class Meta:
        db_table = 'person'
    author = models.ForeignKey(User, )
    first_name = models.CharField( verbose_name="Имя",  max_length=150, )
    last_name = models.CharField(verbose_name="Фамилия", max_length=150, )
    date_of_births = models.DateField( verbose_name="Дата рождения",max_length=10, )
    timestamp = models.DateTimeField(verbose_name="Дата создания", auto_now_add=True, )
    time_update = models.DateTimeField(verbose_name="Дата обновления",auto_now=True, )

The form
class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ( 'first_name', 'last_name', 'date_of_births', )

View in which I edit
def edit_person(request, person_id):
    person = get_list_or_404(Person, id=person_id)
    if request.method == "POST":
        person_form = PersonForm(request.POST, instance=person)
        if person_form.is_valid():
            person_f = person_form.save(commit=False)
            person_f.author = request.user
            person_f.save()
            messages.success(request, "Запись в базе данных обновлена!")
            return redirect('/')
    else:
        person_form = PersonForm(instance=Person)
    context = { 'person_form': person_form,}
    return render(request, 'anketa/edit_person.html', context)

Urly
urlpatterns = [
    ...
    url(r'^(?P<person_id>\d+)/edit/$', a_views.edit_person, name='edit_person'),
    ...
]

I get to the editing page, but I see the following:
aff48d80c3a74f4db5a745f2fe56f977.JPG
And if I change the values ​​​​in the form on the page and try to save, I get an error
AttributeError: 'list' object has no attribute '_meta'

Help correct mistakes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Goryachev, 2017-04-12
@vava0798

https://jsfiddle.net/webirus/stycx5y3/

.block {
  width: 100px;
  height: 100px;
  line-height: 100px;
  margin-left: 100px;
  border: 2px solid #000;
}
.block span {
  position: relative;
  margin-left: -50px;
  font-size: 50px;
  background: #FFF;
}

R
Ruslan Galiev, 2017-04-12
@ruslanredo

codepen.io/ruslanredo/pen/jmObGp

div{
  border:4px solid black;
  width:200px;
  height:200px;
  position:relative;
}

div:before{
  content:"2009";
  position:absolute;
  font-size:80px;
  background:white;
  left:-20px;
  top:55px;
}

O
Overfinch, 2017-04-12
@Overfinch

https://jsfiddle.net/q0rghheo/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question