django template page output No module named blog - python

Im starting with django but this get me one error and i cant run /blog/templates/index.html
ps:
i tried with
url(r'^$', 'FirstBlog.blog.views.home', name='home')
or
url(r'^$', include('views.home'))
but this dont work
i also tried
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
but....
can u help me?
full project: http://www.megafileupload.com/Uih/FirstBlog.tar.gz
django version: 1.7.7

First make sure you set TEMPLATES_DIRS in settings.py, it needs to be an absolute path.
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )
Next if your templates folder is within your app, all you need is the string 'index.html',
# Function Based View
from django.shortcuts import render
def home(request):
return render(request, 'index.html') # Render syntax requires (request, template)
# Class based view
from django.views import generic
class Home(generic.TemplateView):
template_name = 'index.html'
For class based views urls:
from blog.views import Home
urlpatterns = [
url(r'^$', Home.as_view(), name='home'),
]
For functions based views urls:
url(r'^$', 'blog.views.home', name='home')

Related

Django - ModuleNotFoundError (basic app to print Hello in browser)

I'm new to django so take me easy. I'm just following some youtube tutorials and try to make a simple app to print Hello on the browser with django. And I keep getting this error in the urls.py file
ModuleNotFoundError: No module named 'app'
I get this error in urls.py file of my project which is bellow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('app.urls')),
path('admin/', admin.site.urls),
]
I also made an app called app
app\urls.py
from django.urls import path
import views
urlpatterns = [
path('', views.home, name = 'home'),
]
app\views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello!')
I read I think all threads on this topic and nothing helped so far and I can't realise where's my mistake or what I did wrong.
In your settings.py, add 'app.apps.AppConfig', in INSTALLED APP. You have to register the newly made apps to settings.py.
INSTALLED_APPS = [
'app.apps.AppConfig', // Added the name of app
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
]
*Note:- Everytime you add an app, register it in settings.py

Page not found (404) in Django view

I am trying to wire up a view based on the Django tutorial (1.8), and am for some reason not getting a basic url to work:
Page not found (404)
Settings
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'events',
)
In the main folder, I have these_events/these_events/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^/', include('events.urls')),
]
In the events app, I have these_events/events/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r"^$", views.search_db, name='search-db')
]
these_events/events/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def search_db(request):
return HttpResponse("hello, world")
This has me befuddled as I followed the example, and this is the way I remember using Django in the past.
In these_events/these_events/urls.py
try changing
url(r'^/', include('events.urls')),
to
url(r'', include('events.urls')),

ImproperlyConfigured error with include to other url file

I'm using django-cookiecutter to bootstrap my project which will include api paths. Following the steps in Two Scoops of Django 1.11 to configure my urls to follow a similar pattern to this:
api/foo_app/ # GET, POST
api/foo_app/:uuid/ # GET, PUT, DELETE
api/bar_app/ # GET, POST
api/bar_app/:uuid/ # GET, PUT, DELETE
When I try to setup my project like this I'm getting the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'my_project.core.api_urls' from /Users/username/Development/my_project/my_project/core/api_urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
My current setup:
my_project.config.settings.base.py
ROOT_URLCONF = 'config.urls'
DJANGO_APPS = [
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Useful template tags:
# 'django.contrib.humanize',
# Admin
'django.contrib.admin',
]
THIRD_PARTY_APPS = [
'crispy_forms', # Form layouts
'allauth', # registration
'allauth.account', # registration
'allauth.socialaccount', # registration
'rest_framework',
]
# Apps specific for this project go here.
LOCAL_APPS = [
# custom users app
'my_project.users.apps.UsersConfig',
'my_project.core.apps.CoreConfig',
]
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
my_project.config.urls.py
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
urlpatterns = [
url(r'^users/', include('my_project.users.urls', namespace='users')),
url(r'^api/', include('my_project.core.api_urls', namespace='api')),
]
my_project.core.api_urls.urls
from django.conf.urls import url
from django.views.defaults import page_not_found
urlpattenrs = [
url(
regex=r'^users/$',
view=page_not_found,
),
]
The core app does not contain any models right now. It's just where I'm organizing all the URLs
"urlpattenrs" should be "urlpatterns" (in my_project.core.api_urls.urls) ;)

Django cant import my app in urls.py

I have issues importing my app in url patterns, but everything seems in place. I get errors in from fairy_app import views and an error name 'News' is not defined
directory:
fairy-
fairy-
_init_.py
settings.py
urls.py
wsgi.py
fairy_app-
_init_.py
admin.py
models.py
tests.py
views.py
db.fairy
manage.py
views.py
from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.
class News(TemplateView):
template_name = 'news.html'
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from fairy_app.views import News
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'fairy.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', News.as_view()),
)
setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fairy_app',
)
In your urls.py you reference the News view without importing it. You import the views file as module.
So you can either do:
views.News.as_view()
or:
from fairy_app.views import News
2nd way is shorter but gets inconvenient if you have many view classes, so I prefer the first one.
I found the same problem,but got sorted it out.
Create a new python file in app-level for urls
use include function in fairy-/urls file to include all other urls of the app and import views in the new file
for example;
#in url.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^greet/',include('greetings.url')),
]
#created a new file(url.py) in greetings app
#in greetings/url.py
from . import views
urlpatterns = [
url(r'^$',views.greet),
]
#greet is the function in my views`
I had the same problem. I worked around it by giving the full path
from fairy.fairy_app.views
alternatively
from ..fairy_app.views

Django urls giving 404 error

Django is giving me a 404 error whenever I try to access "blog/" on my site, but I've defined the URLs I want and they should be matching that.
Main urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
from blog import views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mySiteProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
blog.urls.py:
from django.conf.urls import patterns,url
from blog import views
urlpatterns = patterns(
url(r'^$',views.index,name='index')
)
404 page:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/blog/
Using the URLconf defined in mySiteProject.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, blog/, didn't match any of these.
Site structure:
mySiteProject
blog
admin.py
models.py
tests.py
views.py
urls.py
__init__.py
mySiteProject
wsgi.py
settings.py
urls.py
__init__.py
manage.py
db.sqlite3
Installed apps:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog'
)
patterns requires a prefix as its first argument followed by zero or more arguments. So this:
urlpatterns = patterns(url(r'^$',views.index,name='index')) # won't work
in blog.urls.py should look like this:
urlpatterns = patterns('', url(r'^$', views.index, name='index')) # now has a prefix as first argument
In its present state, the patterns function in blog.urls.py will return an empty pattern_list, which means that url(r'^blog/', include('blog.urls')) will return no patterns.

Categories

Resources