The view conversation.views.new_conversation didn't return an HttpResponse object. It returned None instead?

I am getting close to the end of the video tutorial by Code with Stein- Learn Django by building an online Market place. Timestamp - 2:07:44. When I run the server the dashboard opens. However when I click on inbox so as to ‘Contact Seller’ I get this error message

ValueError at /inbox/new/24

The view conversation.views.new_conversation didn’t return an HttpResponse object. It returned None instead.
Here is the views.py file -

from django.shortcuts import render, get_object_or_404, redirect

from item.models import Item

from .forms import ConversationMessageForm
from .models import Conversation

def new_conversation(request, item_pk):
    item = get_object_or_404(Item, pk=item_pk)

    if item.created_by == request.user:
        return redirect('dashboard:index')
    
    conversations = Conversation.objects.filter(item=item).filter(members__in=[request.user.id])
    
    if conversations:
        pass # redirect to conversation

    if request.method == 'POST':
        form = ConversationMessageForm(request.POST)

        if form.is_valid():
            conversation = Conversation.objects.create(item=item)
            conversation.members.add(request.user)
            conversation.members.add(item.created_by)
            conversation.save()

            conversation_message = form.save(commite=False)
            conversation_message.conversation = conversation
            conversation_message.created_by = request.user
            conversation_message.save()

            return redirect('item:detail', pk=item_pk)
        else:
            form = ConversationMessageForm()

        return render(request, 'conversation/new.html', {
            'form': form
        })




Check the indentation of your view function code compared to the tutorial. This is almost certainly a case of something being incorrectly indented (or not being indented).

1 Like

Thanks. Let me check.

Hi Ken. I followed your suggestion and went over my code. Still didn’t work. It gave me an error saying Template Does Not Exist. So I then went over my code and discovered that I misspelled conversation in the templates directory. Now my code works. Thanks for your help

Hi Ken I have built the form and now when I click send the form is supposed to send me back to the detail page. It does not do so. Instead it throws an error. This is the error page


Page not found (404)
Request Method: 	POST
Request URL: 	http://127.0.0.1:8000/inbox/new/

Using the URLconf defined in puddle.urls, Django tried these URL patterns, in this order:

    [name='index']
    contact/ [name='contact']
    signup/ [name='signup']
    login/ [name='login']
    items/
    dashboard/
    inbox/ new/<int:item_pk> [name='new']
    admin/
    ^media/(?P<path>.*)$

The current path, inbox/new/, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

How can I fix this so that when I click ‘Send’ it redirects me back to the detail page just like in the tutorial?

This is the conversation.urls.py file

from django.urls import path

from . import views

app_name = 'conversation'

urlpatterns = [
    path('new/<int:item_pk>', views.new_conversation, name='new'),
]

and this is the puddle.urls.py file

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

# from core.views import index, contact

urlpatterns = [
    path('', include('core.urls')),
    # path('', index, name="index"),
    path('items/', include('item.urls')),
    # path('contact/', contact, name="contact"),
    path('dashboard/', include('dashboard.urls')),
    path('inbox/', include('conversation.urls')),
    path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

We’ll need to see the current (corrected) version of the view along with the template being rendered (conversation/new.html).

Here is the conversation/new.html

{% block title %}New conversation{% endblock title %}

{% block content %}
<h1 class="mb-6 text-3xl">New conversation to contact Seller</h1>
<form action="." method="post">
    {% csrf_token %}

    <div class="space-y-4">
        {{ form.as_p }}
    </div>

    {% if form.errors or form.non_field_errors %}
        <div class="mb-3 p-6 bg-red-100 rounded-xl">
            {% for field in form %}
                {{ field.errors }}
            {% endfor %}

            {{ form.non_field_errors }}
        </div>
    {% endif %}

    <button class="mt-6  py-4 px-8 text-lg  bg-teal-500 hover:bg-teal-700 rounded-xl text-white">Send</button>
</form>
{% endblock content %}

and this is the conversation/views.py file

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect

from item.models import Item

from .forms import ConversationMessageForm
from .models import Conversation

@login_required
def new_conversation(request, item_pk):
    item = get_object_or_404(Item, pk=item_pk)

    if item.created_by == request.user:
        return redirect('dashboard:index')
    
    conversations = Conversation.objects.filter(item=item).filter(members__in=[request.user.id])
    
    if conversations:
        return redirect('converstion:detail', pk=conversations.first().id)

    if request.method == 'POST':
        form = ConversationMessageForm(request.POST)

        if form.is_valid():
            conversation = Conversation.objects.create(item=item)
            conversation.members.add(request.user)
            conversation.members.add(item.created_by)
            conversation.save()

            conversation_message = form.save(commit=False)
            conversation_message.conversation = conversation
            conversation_message.created_by = request.user
            conversation_message.save()

            return redirect('item:detail', pk=item_pk)
    else:
        form = ConversationMessageForm()

    return render(request, 'conversation/new.html', {
        'form': form
    })

@login_required
def inbox(request):
    conversations = Conversation.objects.filter(members__in=[request.user.id])

    return render(request, 'conversation/inbox.html', {
        'conversations': conversations
    })

@login_required
def detail(request, pk):
    conversation = Conversation.objects.filter(members__in=[request.user.id]).get(pk=pk)

    if request.method == 'POST':
        form = ConversationMessageForm(request.POST)

        if form.is_valid():
            conversation_message = form.save(commit=False)
            conversation_message.conversation = conversation
            conversation_message.created_by = request.user
            conversation_message.save()

            conversation.save()

            return redirect('conversation:detail', pk=pk)
    else:
        form = ConversationMessageForm()

    return render(request, 'conversation/detail.html', {
        'conversation': conversation,
        'form': form
    })

You have posted multiple views here.

Which view are you looking at, when you click the “send” button and it generates the error?

I don’t understand. How do I get to know which view I am looking at? I think I am looking at conversation/views.py

Checking my code I see that I do not have a conversation/inbox.html file nor a conversation/detail.html file. I have gone over the tut but do not know when those files were created. I think I will go over the video again to find out when those files were created.

In your views.py file, you are showing three views: new_conversation, inbox, and detail. You went to one of those views to see the page that you’re talking about, with the form that you submitted.

That is the view I need you to identify.

I will go with this view:-

@login_required
def detail(request, pk):
    conversation = Conversation.objects.filter(members__in=[request.user.id]).get(pk=pk)

    if request.method == 'POST':
        form = ConversationMessageForm(request.POST)

        if form.is_valid():
            conversation_message = form.save(commit=False)
            conversation_message.conversation = conversation
            conversation_message.created_by = request.user
            conversation_message.save()

            conversation.save()

            return redirect('conversation:detail', pk=pk)
    else:
        form = ConversationMessageForm()

    return render(request, 'conversation/detail.html', {
        'conversation': conversation,
        'form': form
    })

Hi Ken. I don’t know what I am doing. But I checked my code. I found a line of code that did not match the others. I then changed the indentation in my urls.py file and somehow the code is working. Thanks for your help. I am a slow learner. Plodding along. This tutorial ends in about 1 minutes. I think I will do another tut. Maybe by W3School. I think as I get to do more tuts and exercises I will gradually get the hang of django. Thanks once again.

It doesn’t hurt to do so. Check out the resources identified in the Educational section of Awesome Django for recommendations.

No. Don’t do this one. When it comes to Django information, avoid that site.

Select resources from that page I linked above.