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

**URL:** <https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975>\
**Category:** Using Django\
**Created:** [August 18, 2024, 9:38pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975 "2024-08-18T21:38:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 18, 2024, 9:38pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/1 "2024-08-18T21:38:59Z")

</div>

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

 ![Part 5 Write first app](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/6/a/6a6b5db50012be7ab6118560b3a11a4d701fd31a.png)  
Please help what am I missing here"

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 18, 2024, 9:46pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/2 "2024-08-18T21:46:38Z")

</div>

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](https://docs.djangoproject.com/en/5.0/intro/tutorial05/#the-django-test-client)/

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 19, 2024, 12:17am UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/3 "2024-08-19T00:17:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 8:19am UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/4 "2024-08-19T08:19:06Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [August 19, 2024, 11:00am UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/5 "2024-08-19T11:00:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 1:13pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/6 "2024-08-19T13:13:21Z")

</div>

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
> > > 
> > > ```auto
> > > 
> > > ```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 19, 2024, 1:19pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/7 "2024-08-19T13:19:40Z")

</div>

Please post your current index view.

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 1:57pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/8 "2024-08-19T13:57:29Z")

</div>

````from
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, ))) ```
````

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 1:59pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/9 "2024-08-19T13:59:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 19, 2024, 2:08pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/10 "2024-08-19T14:08:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 2:41pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/11 "2024-08-19T14:41:48Z")

</div>

```from
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"),
]

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 19, 2024, 2:44pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/12 "2024-08-19T14:44:11Z")

</div>

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

> [@ZacAmata](#):
>
> ` context_object_name = "latest_question_list",`

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 3:27pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/13 "2024-08-19T15:27:37Z")

</div>

Thanks. Let me remove it and try again.

---

<div class="post-metadata">

**Author:** ![ZacAmata](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zacamata/32/21766_2.png) [@ZacAmata](https://forum.djangoproject.com/u/ZacAmata)\
**Post date:** [August 19, 2024, 4:00pm UTC](https://forum.djangoproject.com/t/how-can-i-fix-importerror-cannot-import-name-reverse-from-django-utils/33975/14 "2024-08-19T16:00:20Z")

</div>

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).
