I am moving my Django app from v1.1 to v1.2.1
However I am stuck on a TemplateSyntax error as seen below. I have not made any other changes to my app
TemplateSyntaxError at /
Caught ImportError while rendering: No module named urls
None of my url reverses in the template seem to work. Here is the culprit line:
{% trans 'Home' %}</div>
It looks like the Template context can't find the url resolver. Here is how my settings look like:
...
ROOT_URLCONF = 'myproject.urls'
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.humanize',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.sitemaps',
'myapp',
)
...
This one I just got myself after making some (fairly sweeping) changes. I suspect I made a mistake somewhere in a form, template, or view, and Django reports a misleading error...
(my pages worked pre-change in 1.2.1)
Related
I'm just getting started with Django, and I'm a touch rusty with web development, so this may be an easy one. I'm stepping through the Django Polls Tutorial from the official documentation and I encounter a problem nearly right away. I'm not having success accessing http://localhost:8000/polls/ . I receive the error...
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
Here is my relevant code...
\mysite\polls\views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
\mysite\polls\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
\mysite\urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
\mysite\mysite\setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
ROOT_URLCONF = 'mysite.urls'
Development server reads...
Not Found: /polls/
[06/Dec/2022 15:02:01] "GET /polls/ HTTP/1.1" 404 2095
I have tried a hodgepodge of fixes that I've seen from other similar tutorial fixes, but nothing has worked and I feel like I'm taking stabs in the dark at this point.
Thank you Hash & Carlos for taking a look.
I modified as suggested, and restarted server, but I get the exact same result...
\mysite\mysite\settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
I feel as though there is a simple path error occurring somehow. Could there have been an error in my django setup? I do see the spaceship at localhost:8000, as I should.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
you are written the code correclty but you need to add your apps in intalled apps in settings.py file
I deleted my project, and my virtual environment(ok because this was the only thing in it), and started over from scratch. This time I was successful. I'm not completely sure why. It is not clear to me where the problem was, but I am ok moving on to bigger and better challenges. Ty for your responses!
BTW: I did not have to add "polls" to the settings.py file, which is consistent with the tutorial
Tying to register my djano app in the settings section of my django project. but when i run the server i am getting an error in the terminal.
django.core.exceptions.ImproperlyConfigured: 'site1app.apps' does not contain a class 'Site1appConfig'. Choices are: 'Site1AppConfig'.
I dont know what's going wrong as i just added it to the list of installed apps in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'site1app.apps.Site1appConfig',
]
This looks like a miss on the capitalization for your class
Change:
'site1app.apps.Site1appConfig'
To:
'site1app.apps.Site1AppConfig'
I have a trouble with plugging recently created app called pages into a django project.
My pages apps.py
from django.apps import AppConfig
class PagesConfig(AppConfig):
name = 'pages'
My settings.py, installed apps:
INSTALLED_APPS = [
'pages.apps.PagesConfig' # pages.apps.pages fails too
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
ImportError: No module named 'pages.apps.PagesConfigdjango';
'pages.apps' is not a package
Does anyonw knows what is wrong there? I did everything according the docs, but django still cannot to plug my app. Any insights appreciated.
You are missing a comma after your entry.
'pages.apps.PagesConfig',
so here is my problem - the gravatar app (from google projects - here! http://code.google.com/p/django-gravatar/) in my django project crashed everything on startup.
i get this error Error: No module named gravatar
this is my installed apps:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'dumpstown.dumpstownapp',
'bootstrap_toolkit',
'registration',
'gravatar'
)
and from the console when i run this:
>> import sys
>> print sys.path
['', 'C:\\development\\python\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'C:\\development\\python\\lib\\site-packages\\pip-1.1-py2.7.egg', 'C:\\developm
ent\\PycharmProjects\\lib\\gravatar', 'C:\\Windows\\system32\\python27.zip', 'C:
\\development\\python\\DLLs', 'C:\\development\\python\\lib', 'C:\\development\\
python\\lib\\plat-win', 'C:\\development\\python\\lib\\lib-tk', 'C:\\development
\\python', 'C:\\development\\python\\lib\\site-packages']
what am i missing here?
UPDATE
huh, weird. I've checked the gravatar folder, and i have
templatetags dir
__init__.py
models.py
views.py
also, if i change the installed apps section to read:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'dumpstown.dumpstownapp',
'bootstrap_toolkit',
'registration',
'templatetags'
)
then the server starts up fine and runs. But i... i'm all confused now! Am i meant to import the templatetags? or something else? how do i refer to the gravatar stuff if i'm importing templatetags??
EDIT: Solved.
I just don't understand how python works, it would seem! I was totally adding the wrong item to the pythonpath - i was adding C:\\development\\PycharmProjects\\lib\\gravatar, where i should have really been adding C:\\development\\PycharmProjects\\lib
this solved the problem, and now i can use gravatar as i want. The settings.py entry is as above in my first example (that is, just 'gravatar') and the way i use it is i just {% load gravatar %}
hurrah!
Possibly you checked out the trunk folder and have a structure like ..\gravatar\gravatar? The setup.py script should install it somewhere your path but you could also try changing C:\developm ent\PycharmProjects\lib\gravatar to C:\developm ent\PycharmProjects\lib\gravatar\gravatar
Totally adding the wrong item to the pythonpath - i was adding C:\development\PycharmProjects\lib\gravatar, where i should have really been adding C:\development\PycharmProjects\lib
My flatpages relevant options in the settings.py look like this:
MIDDLEWARE_CLASSES = (
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.flatpages',
'django.contrib.humanize',
'registration',
)
and in the Backend I added a flatpage with the url set to "/" and one with "/about/. When I call these pages, django shows a 404 error. All my flatpages have a unique template. The "Template Name" entries are looking like this: /flatpages/about.html. What did i miss?
I found it.
I forgot to set the SITE_ID in settings.py correctly.
Do you have a base/default template in place for your flatpages, too? It's easy to miss as they docs don't go into much detail.
Easiest fix is to add /flatpages/default.html to your known templates, basing default.html on the example in the docs.
Or you can point your flatpages to a specific, existing template with the additional options in the admin edit page for a flatpage.
The key statement is changing the SITE_ID in settings.py which
has nothing to do with flatpages - it is a problem that new users
run into when launching into the 'admin' and adding (say) '127..0.0.1' to
the sites menu ( an addition to the default 'example.com' )
In trying to get everything else right, it is easy to overlook
making SITE_ID = 2.
Make 'locahost' the default - get rid of the default tripwire.
Glad I came across this!
Bob