R
R
rumak2017-07-18 17:43:44
Django
rumak, 2017-07-18 17:43:44

Django. How to fix an error when saving a form?

There is a page displaying data of class 'List'. On this page there is a link to another page with a form for editing this data. When trying to save this data in any form, an error pops up:

spoiler
9796140c51064221892062cda31f3500.png
NoReverseMatch at /job/1/edit/
Reverse for 'about_me' not found. 'about_me' is not a valid view function or pattern name.

Code:
models.py
class List(models.Model):
    first = models.CharField(max_length=50)
    second = models.CharField(max_length=50)
    patronymic = models.CharField(max_length=50)
    birthday = models.DateField()
    phone = models.CharField(max_length=12)

    def __str__(self):
        return self.second

forms.py
class ListForm(forms.ModelForm):
    class Meta:
        model = List
        fields = ('first', 'second', 'patronymic', 'birthday', 'phone',)

views.py
def about_me(request, pk):
    about = get_object_or_404(List, pk=pk)
    template = 'job/about_me.html'
    body = {'about': about}
    return render(request, template, body,)

def edit_me(request, pk):
    about = get_object_or_404(List, pk=pk)
    if request.method == "POST":
        form = ListForm(request.POST, instance=about)
        if form.is_valid():
            about = form.save(commit=False)
            about.save()
            return redirect('about_me', pk=about.pk)
    else:
        form = ListForm(instance=about)
    return render(request, 'job/about_form.html', {'form': form})

urls.py
urlpatterns = [
    url(r'^(?P<pk>[0-9]+)/edit/$', views.edit_me, name='edit_me'),
    url(r'^(?P<pk>[0-9]+)/$', views.about_me, name='about_me'),
]

about_me.html
{% extends 'job/base.html' %}

{% block content %}
        <h1>Личная информация:</h1>
        <p>Имя: {{ about.first }}</p>
        <hr>
        <p>Фамилия: {{ about.second }}</p>
        <hr>
        <p>Отчество: {{ about.patronymic }}</p>
        <hr>
        <p>Дата рождения: {{ about.birthday }}</p>
        <hr>
        <p>Номер телефона: {{ about.phone }}</p>
        <hr>
        <a class="btn btn-default" href="{% url 'job:edit_me' pk=about.pk %}"><span class="glyphicon glyphicon-pencil">Изменить</span></a>
{% endblock %}

about_form.html
{% extends 'job/base.html' %}

{% block content %}
    <h1>Редактирование информации о себе</h1>
    <form method="POST" class="edit-me">{% csrf_token %}
            {{ form.as_p }}
            <button type="submit" class="save btn btn-default">Сохранить</button>
    </form>
{% endblock %}

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tema_sun, 2017-07-18
@rumak

Judging by the urls.py shown, you include it somewhere with the job namespace. And in the view you are trying to use the name without specifying the namespace. {% url 'job:edit_me' pk=about.pk %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question