📝 Django

Django UpdateView

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
74
🌿
Level
Medium

UpdateView is a class-based view for editing an existing object through a form.

Basic Example

from django.views.generic import UpdateView
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Task

class TaskUpdateView(LoginRequiredMixin, UpdateView):
    model = Task
    fields = ['title', 'description', 'status', 'priority']
    template_name = 'tasks/task_form.html'
    success_url = reverse_lazy('task-list')

URL

path('tasks/<int:pk>/edit/', views.TaskUpdateView.as_view(), name='task-edit'),

Template (same as for CreateView)

<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Save</button>
  <a href="{% url 'task-list' %}">Cancel</a>
</form>

Permission Check (owner only)

class TaskUpdateView(LoginRequiredMixin, UpdateView):
    model = Task
    fields = ['title', 'description', 'status', 'priority']

    def get_queryset(self):
        return Task.objects.filter(owner=self.request.user)

    def get_success_url(self):
        return reverse('task-detail', kwargs={'pk': self.object.pk})

Additional Processing on Save

class TaskUpdateView(SuccessMessageMixin, UpdateView):
    model = Task
    fields = ['title', 'status', 'priority']
    success_message = 'Task updated!'

    def form_valid(self, form):
        # Log the change
        logger.info(f'Task {self.object.pk} updated by {self.request.user}')
        return super().form_valid(form)

Custom Form Fields

class TaskUpdateView(UpdateView):
    model = Task
    form_class = TaskUpdateForm  # custom form instead of fields

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user  # pass the user to the form
        return kwargs

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 134
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 85
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 73

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!