Reputation: 510
I am a Django developer with a quick question. I have the following code in which I have created a model 'Plan', which has several fields and I have also created a view for it. However, when I submit the form for the create view, I get an error saying that it could not find a url to redirect to. I have tried adding the 'get_absolute_url' method to the model and I have also tried adding the 'success_url' attribute to the create view, but I always get the following error: The view work.views.PlansCreateView didn't return an HttpResponse object. It returned None instead
Models.py
from django.db import models
from django.utils import timezone
from datetime import timedelta, datetime
from django.contrib.auth.models import User
from django.utils import timezone
from django.urls import reverse, reverse_lazy
from django.shortcuts import render, redirect
date = datetime.today() + timedelta(days=10)
class Plan(models.Model):
title = models.CharField(max_length=100)
description = models.CharField(max_length=300)
date_created = models.DateTimeField(default=timezone.now)
date_deadline = models.DateTimeField(default=date)
supervisor = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f"{self.supervisor.username} {self.title}"
Project Urls.py
urlpatterns = [
# Core
path('admin/', admin.site.urls),
path('', HomeView.as_view(), name="core-home"),
path('about/', AboutView.as_view(), name="core-about"),
# Users
path('register/', register, name="users-register"),
path('login/', LoginView.as_view(template_name="users/login.html"), name="users-login"),
path('logout/', LogoutView.as_view(template_name="users/logout.html"), name="users-logout"),
# Work
path('plans/', PlansListView.as_view(), name="work-plans"),
path('<int:pk>/tasks', TasksListView.as_view(), name="work-tasks"),
path('plans/create/', PlansCreateView.as_view(), name="work-plans-create")
]
Template for Create View
{% extends 'core/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="work-plans-create">
<form method="POST" action="">
{% csrf_token %}
<h2>Plan</h2>
{{ form|crispy }}
<button type="submit" class="button-filled">Create Plan</button>
</form>
</div>
{% endblock content %}
Create View
class PlansCreateView(LoginRequiredMixin, CreateView):
model = Plan
fields = ["title", "description", "date_deadline"]
template_name = "work/plans_forms.html"
success_url = 'work-plans/'
def form_valid(self, form):
form.instance.supervisor = self.request.user
super().form_valid(form)
My objective is to redirect to the url named 'work-plans' (check the urls.py file). Thank you everyone in advance.
P.S. I know there are similar questions to this one already on the stack but they did not solve my problem.
P.S.S The 'plans' are being created successfully (I looked at the admin page). The only problem is that it doesn't know where to redirect to.
Upvotes: 0
Views: 854
Reputation: 1910
To fix this error The view work.views.PlansCreateView didn't return an HttpResponse object
you need to return from form_valid
method.
def form_valid(self, form):
form.instance.supervisor = self.request.user
return super().form_valid(form)
Also, your success_url
parameter is wrong, see @markwalker_'s answer.
Upvotes: 1
Reputation: 12849
Use a reverse on the URL by name;
from django.urls import reverse_lazy
class PlansCreateView(LoginRequiredMixin, CreateView):
model = Plan
fields = ["title", "description", "date_deadline"]
template_name = "work/plans_forms.html"
success_url = reverse_lazy("work-plans")
This will make django lookup the URL from your URL patterns.
Upvotes: 1