没有名为django.shortcuts的模块

时间:2015-02-01 16:39:01

标签: python django

我正在创建一个Django项目,但我遇到了一个问题。在我的views.py文件中,我在Ubuntu上的Wing IDE调试器说:

File "/home/deanna/django_test/article/views.py", line 1, in <module>
from django.shortcuts import render_to_response

ImportError: No module named django.shortcuts

这是我的views.py文件:

from django.shortcuts import render_to_response
from article.models import Article
from django.http import HttpResponse
#import pdb; pdb.set_trace()
# Create your views here.

def articles(request):
    language = 'en-us'
    session_language = 'en-us'

    if 'lang' in request.COOKIES:
        language = request.COOKIES['lang']

    return render_to_response('articles.html',  
                          {'articles':
                           Article.objects.all(), 'language' : language })

def article(request, article_id=1):
    return render_to_response('article.html', {'article': 
  Article.objects.get(id=article_id)}}                                    
#The line above is supposed to be properly indented but Stack Overflow isn't letting me do that.

def language(request, language='en-us'):
    response = HttpResponse("setting language to %s" % language)


    response.set_cookie('lang', language)

    return response

当我在localhost上运行网页时,我收到此错误:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/articles/all/

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

    ^all/$
    ^get/(?P<article_id>\d+)/$
    ^language/(?P<language>[a-z\-]+)/$

The current URL, articles/all/, 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.

我创建了一个urls.py文件和一个settings.py文件。两者都没有错误。我还有一个articles.html和一个article.html文件,它们也没有错误。 文件的文件路径:

urls.py- /home/(my name)/django_test/article
views.py- /home/(my name)/django_test/article
settings.py- /home/(my name)/django_test/django_test
articles.html- /home/(my name)/django_test/article/templates
article.html- /home/(my name)/django_test/article/templates

我将django.shortcuts下载到我的django_test文件中,但我不知道调试器为什么抱怨或为什么我的网页没有加载。谢谢。

1 个答案:

答案 0 :(得分:0)

当您遇到这些类型的错误时:

ModuleNotFoundError: No module named 'django.shortscuts'

首先检查拼写是否正确:

from django.http import HttpResponse
from django.shortcuts import redirect