How to solve 'module' object is not iterable in django

Viewed 226

I have created a Django application and initially the code worked perfectly but when I restarted the development server I started having 'module' object not iterable exception. This is my code

views.py

import datetime
from django.contrib import messages
from django.shortcuts import render
from forms.forms import *
from django.core.mail import send_mail
from django.http import HttpResponse
from patients.models import *


def doctor_view(request, id):
    patients = Patients.objects.get(id=id)
     if request.method == "POST":
            form = DoctorForm(request.POST)
            if form.is_valid():
                f = form.save(commit=False)
                date = datetime.datetime.now()
                f.date_seen = date
                f.save()
                messages.info(request, 'patient profile was successfully updated !!!')
        else:

            form = DoctorForm(instance=patients)
            date = datetime.datetime.now()
        return render(request, 'doctors/templates/patient_registration.html',
                      {'form': form, 'title': 'Patient Treatment', 'patients': patients, 'date': date, 'messages': messages})

urls.py

from django.urls import path

from . import views


urlpatterns = [
    path('<int:id>/', views.doctor_view, name='doctor-views'),
  
]

Traceback

Internal Server Error: /doctor/1/
Traceback (most recent call last):
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\doctors\views.py", line 42, in doctor_view
    {'form': form, 'title': 'Patient Treatment', 'patients': patients, 'date': date, 'messages': messages})
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\SERU\Desktop\School Projects\DAS_project\venv\lib\site-packages\django\template\defaulttags.py", line 167, in render
    values = list(values)
TypeError: 'module' object is not iterable

I am passing the values to the template using a dictionary and my html template is as follows. I have also installed django-crispy-forms.

{% extends 'patients/templates/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form action="" method="post">
    {% csrf_token %}

    <div class="col-sm-12 col-md-12 col-lg-12 d-flex flex-column" style="margin-lef:auto; align-items:center; justify-content:center">
           <div class="col-sm-12 col-md-8 col-lg-8">
               <h4 class="mt-3 mb-2">Patient Treatment</h4>
               <hr class="my-4">
           </div>
            <div class="col-8 d-flex" style="border:1px solid #dfdfdf;z-index:20;color:maroon">
                <h1 class="lead" style="margin:20px;">Treating: </h1>
                <h6 class=" lead " style="margin:20px;" >Name:   {{ patients.name}}</h6> <h6 class=" lead " style="margin-top:20px;" >{{ patients.surname}}</h6>
                <h6 class=" lead " style="margin:20px;" >Nat ID:    {{ patients.id_no}}</h6> <h6 class=" lead " style="margin-top:20px;" >Gender:    {{ patients.gender}}</h6>
                 {% for message in messages %}
                <div  class="alert alert-{{ message.tags }}" role="alert" style="color:#0e466b;;"> {{ message }}</div>
                {% endfor %}
            </div>
            <div class="col-sm-12 col-md-8 col-lg-8">
                {{ form.symptoms|as_crispy_field }}
           </div>
          <div class="col-sm-12 col-md-8 col-lg-8">
                {{ form.disease|as_crispy_field }}
           </div>
          <div class="col-sm-12 col-md-8 col-lg-8">
                {{ form.treatment|as_crispy_field }}
           </div>
        <div class="col-sm-12 col-md-8 col-lg-">
                 <input type="submit" value="Send to Doctor" class="btn btn-success btn-sm">
                 <a href="email" class="btn btn-info btn-sm ml-3">Send email</a>
                 <hr class="my-4">
                <center> <h6 class="  " style="margin:20px;" >  {{ date}}</h6></center>
            </div>
    </div>

  <!-- Button to Open the Modal -->


  <!-- The Modal -->

    </div>
</form>
{% endblock %}```
1 Answers

The problem is in your context dictionary. Django automatically adds a messages object to the request context without you needing to add it manually (assuming you have MessageMiddleware enabled). What you're doing at the end of your dictionary is overriding that object with the module messages that you imported at the top of your file.

Simply removing the key-value pair 'messages': messages from your dictionary should fix the issue.

# ...
return render(request, 'doctors/templates/patient_registration.html',
                      {'form': form, 'title': 'Patient Treatment', 'patients': patients, 'date': date})
Related