Imperator123
Imperator123

Reputation: 609

ImportError: No module named post_home

I was trying to get my HttpResponse from my views.py

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.


def post_home(request):
    return HttpResponse("<h1>GWorld</h1>")

So i tried to import it from my folder app to the urls.py which is located in the other folder

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^posts/$', include("posts.views.post_home")),
]

But then i received an error

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named post_home

I'm not really sure what happen because i'm pretty sure that i wrote the function in the views.py file.

i tried using

url(r'^posts/', include("posts.views.post_home")),

But i received the same error.

Upvotes: 0

Views: 393

Answers (1)

knbk
knbk

Reputation: 53699

include() looks for a module with the given name. That module must contain more url patterns that will be included in the main urlconf.

post_home is a view function. You need to import it and pass it to the url pattern without using include.

from post.views import post_home

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^posts/$', post_home)),
]

Upvotes: 4

Related Questions