Django - ImportError at /blog。没有模块名为url

时间:2021-01-29 20:26:39

I'm new to django and in the process of learning. While doing some tutorial exercises I ran into some errors which I need help with.

我是django的新手,在学习的过程中。在做一些指导练习时,我遇到了一些我需要帮助的错误。

Error: ImportError at /blog. No module named urls

错误:ImportError在/博客。没有模块名为url

The urls.py file under the app named 'blog' is

url。在名为“blog”的应用程序下的py文件。

from django.conf.urls.defaults import patterns, include, url
from mysite.blog.views import archive

urlpatterns = patterns('',
    url(r'^$',archive),
                       )

The views.py file under the app named 'blog' is

视图。在名为“blog”的应用程序下的py文件。

# Create your views here.
from django.template import loader, Context
from django.http import HttpResponse
from mysite.blog.models import Blogpost

def archive(request):
    posts = BlogPost.objects.all()
    t = loader.get_template("archive.html")
    c = context({'posts': posts })
    return HttpResponse(t.render(c))

The urls.py file under the project 'mysite' is

url。在“mysite”项目下的py文件。

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
url(r'^blog/', include('mysite.blog.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)

Traceback:

回溯:

File "D:\Super Developer\Python\lib\site-packages\django\core\handlers\base.py" in get_response
  89.                     response = middleware_method(request)
File "D:\Super Developer\Python\lib\site-packages\django\middleware\common.py" in process_request
  67.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in is_valid_path
  531.         resolve(path, urlconf)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in resolve
  420.     return get_resolver(urlconf).resolve(path)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in resolve
  298.             for pattern in self.url_patterns:
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "D:\Super Developer\Python\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /blog
Exception Value: No module named urls

Structure of Project/App:

项目/应用程序的结构:

  1. mysite
    • manage.py
    • jobs
    • init.py
    • init.py
    • settings.py
    • settings.py
    • urls.py
    • urls . py
    • wsgi.py
    • wsgi.py
    • blog
      • init.py
      • init.py
      • models.py
      • models.py
      • views.py
      • views.py
      • urls.py
      • urls . py
      • tests.py
      • tests.py
      • Templates - archive.html
      • 模板——archive.html
    • 博客init。py模型。py视图。py url。py测试。py模板——archive.html
  2. mysite管理。py init。py设置。py url。py wsgi。py博客init。py模型。py视图。py url。py测试。py模板——archive.html

Python path

Python的路径

['D:\Super Developer\Proj\mysite', 'C:\Windows\system32\python27.zip', 'D:\Super Developer\Python\DLLs', 'D:\Super Developer\Python\lib', 'D:\Super Developer\Python\lib\plat-win', 'D:\Super Developer\Python\lib\lib-tk', 'D:\Super Developer\Python', 'D:\Super Developer\Python\lib\site-packages']

[' D:\超级开发者\项目\ mysite”,“C:\ Windows \ system32系统\ python27。zip', 'D:\Super Developer\Python\ dll ', 'D:\超级开发者\Python\lib', 'D:\超级开发者\ \ \ \ \ \ \ \ - \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \

8 个解决方案

#1


5  

url(r'^blog/', include('mysite.blog.urls')),

This may need to be changed to

这可能需要更改为。

url(r'^blog/', include('blog.urls')),

Note: no 'mysite' prefix.

注意:没有“mysite”前缀。

#2


1  

In blog's urls.py, you import

在博客的url。py,你进口

from django.conf.urls.defaults import patterns, include, url

从django.conf.urls.defaults导入模式,包括,url。

But in project's urls.py, you use

但在项目的url。py,使用

from django.conf.urls import patterns, include, url

从django.conf。url导入模式,包括url。

Is this indented? The latter seems to fail at least in my env.

这是缩进吗?后者似乎至少在我的环境中失败了。

#3


1  

Check if you did the declaraion for media or static urls in your app url file using

检查您是否在应用程序url文件中对媒体或静态url进行了声明。

url(r'^media/(?P<path>.*)$', 'django.views.static.serve'
, {'document_root': settings.MEDIA_ROOT}
  ),

instead of in the application url file.

而不是在应用程序url文件中。

I had the same problem, changed this and I didn't have the problem anymore.

我有同样的问题,改变了这个,我不再有问题了。

#4


0  

Maybe you're missing a __init__.py file in your blog folder.

也许你错过了一个机会。py文件在你的博客文件夹。

#5


0  

I think patterns cannot have comma after last element:

我认为模式不能在最后一个元素之后使用逗号:

urlpatterns = patterns('',
    url(r'^$',archive), <-- delete this comma
)

also try to run manage.py shell and import blog.urls, if there's not raised any exception in there

也试着运行管理。py shell和import blog。url,如果没有引发异常。

#6


0  

Try giving this:

试着把这:

url(r'^blog/', include('mysite.urls')),

#7


0  

A friend of mine was going through some django tutorial and got the same error. The mistake he made was in the file structures. In the new version of django, it creates two directory levels for example mysite/mysite and he tried putting the blog folder in mysite/blog instead of mysite/mysite/blog. After making that change, it all seemed to work well. Try to look at your structure and hope it helps.

我的一个朋友正在经历一个django教程,并得到了同样的错误。他犯的错误在于文件结构。在django的新版本中,它创建了两个目录级别,例如mysite/mysite,他尝试将博客文件夹放到mysite/blog中,而不是mysite/mysite/blog。在做出这种改变之后,一切似乎都很顺利。试着看看你的结构,希望它能有所帮助。

#8


0  

Looks like you are using Django 1.6 or newer. Due to some changes in the framework, this code wouldn't work. Use 1.5 or this code in blog's urls.py:

看起来您使用的是Django 1.6或更新版本。由于框架中的一些更改,此代码不起作用。在博客的urls.py中使用1.5或以下代码:

    from django.conf.urls import patterns, include, url
    from blog import views

    urlpatterns = patterns('',
        url(r'^$', views.archive),
    )

And this in views.py:

这在views.py:

    from django.template import loader, Context
    from django.http import HttpResponse
    from blog import models 

    def archive(request):
        posts = models.BlogPost.objects.all()
        t = loader.get_template('archive.html')
        c = Context({ 'posts': posts })
        return HttpResponse(t.render(c))

#1


5  

url(r'^blog/', include('mysite.blog.urls')),

This may need to be changed to

这可能需要更改为。

url(r'^blog/', include('blog.urls')),

Note: no 'mysite' prefix.

注意:没有“mysite”前缀。

#2


1  

In blog's urls.py, you import

在博客的url。py,你进口

from django.conf.urls.defaults import patterns, include, url

从django.conf.urls.defaults导入模式,包括,url。

But in project's urls.py, you use

但在项目的url。py,使用

from django.conf.urls import patterns, include, url

从django.conf。url导入模式,包括url。

Is this indented? The latter seems to fail at least in my env.

这是缩进吗?后者似乎至少在我的环境中失败了。

#3


1  

Check if you did the declaraion for media or static urls in your app url file using

检查您是否在应用程序url文件中对媒体或静态url进行了声明。

url(r'^media/(?P<path>.*)$', 'django.views.static.serve'
, {'document_root': settings.MEDIA_ROOT}
  ),

instead of in the application url file.

而不是在应用程序url文件中。

I had the same problem, changed this and I didn't have the problem anymore.

我有同样的问题,改变了这个,我不再有问题了。

#4


0  

Maybe you're missing a __init__.py file in your blog folder.

也许你错过了一个机会。py文件在你的博客文件夹。

#5


0  

I think patterns cannot have comma after last element:

我认为模式不能在最后一个元素之后使用逗号:

urlpatterns = patterns('',
    url(r'^$',archive), <-- delete this comma
)

also try to run manage.py shell and import blog.urls, if there's not raised any exception in there

也试着运行管理。py shell和import blog。url,如果没有引发异常。

#6


0  

Try giving this:

试着把这:

url(r'^blog/', include('mysite.urls')),

#7


0  

A friend of mine was going through some django tutorial and got the same error. The mistake he made was in the file structures. In the new version of django, it creates two directory levels for example mysite/mysite and he tried putting the blog folder in mysite/blog instead of mysite/mysite/blog. After making that change, it all seemed to work well. Try to look at your structure and hope it helps.

我的一个朋友正在经历一个django教程,并得到了同样的错误。他犯的错误在于文件结构。在django的新版本中,它创建了两个目录级别,例如mysite/mysite,他尝试将博客文件夹放到mysite/blog中,而不是mysite/mysite/blog。在做出这种改变之后,一切似乎都很顺利。试着看看你的结构,希望它能有所帮助。

#8


0  

Looks like you are using Django 1.6 or newer. Due to some changes in the framework, this code wouldn't work. Use 1.5 or this code in blog's urls.py:

看起来您使用的是Django 1.6或更新版本。由于框架中的一些更改,此代码不起作用。在博客的urls.py中使用1.5或以下代码:

    from django.conf.urls import patterns, include, url
    from blog import views

    urlpatterns = patterns('',
        url(r'^$', views.archive),
    )

And this in views.py:

这在views.py:

    from django.template import loader, Context
    from django.http import HttpResponse
    from blog import models 

    def archive(request):
        posts = models.BlogPost.objects.all()
        t = loader.get_template('archive.html')
        c = Context({ 'posts': posts })
        return HttpResponse(t.render(c))