How can I fix ``` ImportError: cannot import name 'reverse' from 'django.utils' ```

Please help. I am working on Paart 5 Writing your first Django app, part 5. I have entered the code into the interactive Python shll.My code seems to be in line with the tutorial code. However I an getting a traceback error message


Please help what am I missing here"

Welcome @ZacAmata !

First, a side note: Please avoid posting images of code or errors here. Copy/paste the code into the body of your post, between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. Do this for each file or shell session or error you wish to post.

You import reverse from django.urls, not django.utils. See the text and examples starting at The Django test client/

it seems “django.urls”, but you write “django.utils”.

@KenWhitesell. Thank you ever so much. Your reply means alot to me. I am a 73 year old Nigerian actor and I am learning Python, Django to keep myself busy and have fun. Just like some other folks watch Manchester City, Arsenal, Real Madrid, Super Bowl(in USA) and Cricket in India, and the UK. I get my joy from learning to ode-JavaScript, Unity Engine, C#, PostgreSQL. Problem is my eyes. I find it hard to focus. Someone says I have cataracts in my eyes. Maybe that is why I have had so much anguish and frustration with errors (TemplateDoes not Exist, Page not found) going through tutorials by Corey Schafer, Dave Gray, John Elder, Mosh Hamedani and Very Academy. I will go back to those tuts (after I finish with Writing my first Django app) and check if I made the same type of mistake(my eyes most probably). Thank you. I will now continue with Writing my first Django App. I think I am beginning to understand Django.Thanks to the Django docs tutorials.

Mark the post that provided assistance to you as a solution, to be beneficial for the community :slight_smile:

Hi. I am back. Things are not as simple as they look. I typed in the following code to my interactive Python shell and got almost 3 miles of error code. What am i not seeing now?Here is my shell code ```

from django.test.utils import setup_test_environment
setup_test_environment()
from django.test import Client
client = Client()
response = client.get(“/”)
Not Found: /
response.status_code
404
from django.urls import reverse
response = client.get(reverse(“polls:index”))
Internal Server Error: /polls/
Traceback (most recent call last):
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\base.py”, line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\base.py”, line 143, in dispatch
return handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\list.py”, line 174, in get
context = self.get_context_data()
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\list.py”, line 147, in get_context_data
return super().get_context_data(**context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: keywords must be strings
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\test\client.py”, line 1129, in get
response = super().get(
^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\test\client.py”, line 479, in get
return self.generic(
^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\test\client.py”, line 676, in generic
return self.request(**r)
^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\test\client.py”, line 1092, in request
self.check_exception(response)
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\test\client.py”, line 805, in check_exception
raise exc_value
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\base.py”, line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\base.py”, line 143, in dispatch
return handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\list.py”, line 174, in get
context = self.get_context_data()
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\hp\Documents\Learn Python\Django\mdn04\venmdn04\Lib\site-packages\django\views\generic\list.py”, line 147, in get_context_data
return super().get_context_data(**context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: keywords must be strings

Please post your current index view.

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic

from .models import Choice, Question

class IndexView(generic.ListView):
    template_name = "polls/index.html"
    context_object_name =  "latest_question_list",
        
    def get_queryset(self):
        # ***Return thee last five published questions***
        return Question.objects.order_by("-pub_dte")[:5]


class DetailView(generic.DetailView):
        model = Question
        template_name = "polls/detail.html" 


        
class ResultsView(generic.DetailView):
        model = Question
        template_name = "polls/results.html" 
        

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
         selected_choice = question.choice_set.get(pk=request.POST["choice"])
    except (KeyError, Choice.DoesNotExist):
         #Redisplay the question voting form
         return render(request, "polls/detail.html", {
              "question": question,
              "error_message": "You didn't select a choice. Please choose one and select!",
         },)
    else:
         selected_choice.votes = F("votes") + 1
         selected_choice.save()
        #  # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button
    return HttpResponseRedirect(reverse("polls:results", args=(question.id, ))) ```

In the view you will see pub_date as pub_date. I made a mistake and did not know how to change the model so I kept using pub_dte for pub_date. I hope to larn how to change and rename models

Please post the urls.py file from your polls directory.

from . import views

app_name = "polls"
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path("<int:pk>/", views.DetailView.as_view(), name="detail"),
    path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

You’ve got an extra comma - , at the end of this line. It needs to be removed.

1 Like

Thanks. Let me remove it and try again.

Yaaay!!!..,@KenWhitesell… It worked. Thank you for your patience. I relly appreciate it. I have now gotten up to where the response status_code returns 200. I thin I will take a short break. Give my eyes a little rest and come back to continue the tesst module(part 5).