R
R
rumak2017-07-18 23:00:51
Django
rumak, 2017-07-18 23:00:51

Django. How to get rid of "unhashable type: 'dict'"?

Problem: error

unhashable type: 'dict'

spoiler
83aac7b5400c486eaf8721139c16532a.png

Code:
models.py
class Contacts(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)
    email = models.EmailField()
    skype = models.CharField(max_length=255)
    contact = models.CharField(max_length=50)

    def __str__(self):
        return self.second

forms.py
class ContactsForm(forms.ModelForm):
    class Meta:
        model = Contacts
        fields = ('first', 'second', 'patronymic', 'birthday',)

class PhoneContactsForm(forms.ModelForm):
    class Meta:
        model = Contacts
        fields = ('phone',)

class EmailContactsForm(forms.ModelForm):
    class Meta:
        model = Contacts
        fields = ('email',)

class SkypeContactsForm(forms.ModelForm):
    class Meta:
        model = Contacts
        fields = ('skype',)

views.py
def edit_contacts(request, pk):
    contact = get_object_or_404(Contacts, pk=pk)
    phone = get_object_or_404(Contacts, pk=pk)
    email = get_object_or_404(Contacts, pk=pk)
    skype = get_object_or_404(Contacts, pk=pk)

    form = ContactsForm(request.POST, instance=contact)
    if request.method == "POST":
        if form.is_valid():
            contact = form.save(commit=False)
            contact.save()
            return redirect('job:list_contacts', pk=contact.pk)
    else:
        form = ContactsForm(instance=contact)

    form1 = PhoneContactsForm(request.POST, instance=phone)
    if request.method == "POST":
        if form1.is_valid():
            phone = form1.save(commit=False)
            phone.save()
            return redirect('job:list_contacts', pk=phone.pk)
        else:
            form1 = PhoneContactsForm(instance=phone)

    form2 = EmailContactsForm(request.POST, instance=email)
    if request.method == "POST":
        if form2.is_valid():
            email = form2.save(commit=False)
            email.save()
            return redirect('job:list_contacts', pk=email.pk)
        else:
            form2 = EmailContactsForm(instance=email)

    form3 = SkypeContactsForm(request.POST, instance=skype)
    if request.method == "POST":
        if form3.is_valid():
            skype = form3.save(commit=False)
            skype.save()
            return redirect('job:list_contacts', pk=skype.pk)
        else:
            form3 = SkypeContactsForm(instance=skype)

    return render(request, 'job/contacts_form.html', {'form': form}, {'form1': form1}, {'form2': form2}, {'form3': form3})

urls.py
urlpatterns = [
    url(r'^contacts/(?P<pk>[0-9]+)/$', views.list_contacts, name='list_contacts'),
    url(r'^contacts/(?P<pk>[0-9]+)/edit/$', views.edit_contacts, name='edit_contacts'),
    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'),
]

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

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

contacts_form.py
{% extends 'job/base.html' %}

{% block content %}
    <h1>Редактирование информации о контактах</h1>
        <div class="forms">
            <div class="form1">
                <h2>Форма №1</h2>
                <form method="POST" class="edit-contacts">{% csrf_token %}
                    {{ form.as_p }}
                    <button type="submit" class="save btn btn-default">Сохранить</button>
                </form>
            </div>
            <div class="form2">
                <h2>Форма №2</h2>
                <form method="POST" class="edit-contacts">{% csrf_token %}
                    Номер телефона: {{ form1.as_p }}
                    <button type="submit" class="save btn btn-default">Сохранить</button>
                </form>
            </div>
            <div class="form3">
                <h2>Форма №3</h2>
                <form method="POST" class="edit-contacts">{% csrf_token %}
                    Почта: {{ form2.as_p }}
                    <button type="submit" class="save btn btn-default">Сохранить</button>
                </form>
            </div>
            <div class="form4">
                <h2>Форма №4</h2>
                <form method="POST" class="edit-contacts">{% csrf_token %}
                    Скайп: {{ form3.as_p }}
                    <button type="submit" class="save btn btn-default">Сохранить</button>
                </form>
            </div>
            <div class="form5">
                <h2>Форма №4</h2>
                <form method="POST" class="edit-contacts">{% csrf_token %}
                    Скайп: {{ form4.as_p }}
                    <button type="submit" class="save btn btn-default">Сохранить</button>
                </form>
            </div>
        </div>
{% endblock %}

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DDDsa, 2017-07-19
@rumak

Look at the documentation of the render function , you messed up with the arguments.
I assume that you need to use not many dictionaries with one value, but one dictionary with several values

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question