garp_0
garp_0

Reputation: 45

In Django, how to redirect to an UpdateView upon submitting the CreateView?

I've created a single FormView and template HTML that I'm going to use for both creating and updating records from a Model.

However, I cannot figure out how to set the CreateView to redirect to the new primary key that's being created. I've currently set it to go back to the base page, and but I can't seem to find any information on where to start with getting the new or existing primary key to redirect to the UpdateView.

models.py

class NewEmployee(models.Model):
    name = models.CharField(max_length=50)
    position = models.CharField(max_length=50)
    start_date = models.DateField()
    date_entered = models.DateTimeField('date entered', auto_now_add=True)

forms.py

class NewEmpForm(ModelForm):
    class Meta:
        model = NewEmployee
        fields = ['name', 'position', 'start_date']

views.py

class EditView(UpdateView):
    model = NewEmployee
    form_class = NewEmpForm


class Add_Emp(CreateView):
    model = NewEmployee
    form_class = NewEmpForm

urls.py

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('add/', views.Add_Emp.as_view(success_url='../'), name='newemp-add'),
    path('<int:pk>/', views.EditView.as_view(success_url="../"), name='newemp-rev'),
]

newemployee_form.html

<DOCTYPE html>
<html>
<head>
<title>Employee</title>
</head>
<body>

<form method="post" >
{% csrf_token %}
{{form.as_p}}

<button type="submit" class="btn btn-default">Submit</button>
</form>
</body>
</html>

Upvotes: 3

Views: 2819

Answers (5)

JDavies
JDavies

Reputation: 2770

As of Django v3.0 you will need to pass in args like so:

def myview(request):
    return HttpResponseRedirect(reverse('arch-summary', args=[1945]))

From the docs: "If the URL accepts arguments, you may pass them in args. For example:"

Sharing as it tripped me up :)

Upvotes: 0

Igor Belkov
Igor Belkov

Reputation: 446

This article might help:

https://realpython.com/django-redirects/#redirects-in-django

You can write redirect code in is_valid() method.

Upvotes: 0

Alasdair
Alasdair

Reputation: 308979

You can't use success_url to do this, because you need access to the object to get its pk.

The easiest way to do this is to override get_absolute_url for your model.

class NewEmployee(models.Model):
    ...

    def get_absolute_url(self):
        return reverse('newemp-rev', [self.pk])

Then remove success_url from your URL patterns for the create and update views. By default, they will both use the object's get_absolute_url method to get the URL to redirect to.

If you don't want to override get_absolute_url, then override get_success_url for both views, for example:

class Add_Emp(CreateView):
    model = NewEmployee
    form_class = NewEmpForm

    def get_success_url(self):
        return reverse('newemp-rev', [self.object.pk])

Upvotes: 0

Aman Garg
Aman Garg

Reputation: 2547

You can get the object saved in the CreateView by overriding get_success_url

class MyCreateView(CreateView):
    ...
    def get_success_url(self):
        return reverse('newemp-rev', [self.object.pk])

Upvotes: 0

p14z
p14z

Reputation: 1810

In your CreateView you usually override the form_valid method, which is run once the form is validated:

from django.shortcuts import redirect


class Add_Emp(CreateView):
    model = NewEmployee
    form_class = NewEmpForm

    def form_valid(self, form):
        employee = form.save()  # save form
        return redirect('newemp-rev', pk=employee.pk)

Upvotes: 1

Related Questions