我正在创建一个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当我在我的本地主机上运行网页时,我得到了这个错误:
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文件中,但是我不知道为什么调试器会报错,也不知道为什么我的网页不能加载。谢谢。
发布于 2021-01-21 18:14:58
当你面对这些类型的错误时:
ModuleNotFoundError: No module named 'django.shortscuts'首先检查拼写是否正确:
from django.http import HttpResponse
from django.shortcuts import redirecthttps://stackoverflow.com/questions/28265031
复制相似问题