UpdateView not saved

154 views
Skip to first unread message

Dariusz Mysior

unread,
May 27, 2016, 10:29:37 AM5/27/16
to Django users
I try update my photo in avatar field but it not save changes, please look on it

update_form.html

{% extends 'base.html' %}
{% block title %}Edycja profilu{% endblock %}


{% if user.is_authenticated %}
    {% block top_menu %}
    <a href="{% url 'myprofile:profile-view' %}">{{ user.username }}</a>
    <a href="{% url 'users:logout-view' %}">Wyloguj się</a>
    {% endblock %}

{% block content %}
    <form action="" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="zapisz" />
    </form>
{% endblock %}
{% else %}

{% endif %}

views.py

from django.shortcuts import render
from django.views.generic import TemplateView, UpdateView
from users.models import MysiteUser

class ProfileView(TemplateView):
    template_name = 'profile.html'

class EditView(UpdateView):
    model = MysiteUser
    fields = ['avatar']
    pk_url_kwarg = 'pk'
    template_name = 'update_form.html'
    success_url = '/myprofile/'

    def form_valid(self, form):
        self.object = form.save()
        return super(EditView,self).form_valid(form)

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

##################################################

class MysiteUser(AbstractUser):
    avatar = models.ImageField(upload_to="avatar")

    def __str__(self):
        return self.username


Ricardo Daniel Quiroga

unread,
May 27, 2016, 10:49:07 AM5/27/16
to django...@googlegroups.com
add enctype="multipart/form-data" param in you html form tag :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/968faf01-3b6b-43d8-8c98-d1555d48ee4d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Ricardo Daniel Quiroga

Dariusz Mysior

unread,
May 27, 2016, 11:01:24 AM5/27/16
to Django users
Yes it help :), thanks!
Reply all
Reply to author
Forward
0 new messages