Django-CMS Will not run, 404 Page Error on initial installation - django

I am following the tutorial here:
http://django-cms.readthedocs.org/en/2.4.0/getting_started/tutorial.html
I have python 2.7.2 installed and this is all in a virtualenv
my pip install list:
Django (1.5.1)
django-classy-tags (0.4)
django-cms (2.4.2)
django-filer (0.9.5)
django-mptt (0.5.2)
django-polymorphic (0.5.1)
django-reversion (1.7.1)
django-sekizai (0.7)
easy-thumbnails (1.3)
html5lib (1.0b2)
MySQL-python (1.2.4)
PIL (1.1.7)
six (1.3.0)
South (0.8.1)
wsgiref (0.1.2)
Project is named dcms and this folder has the contents
dcms manage.py media static
I have also included media and static folders in dcms because I was confused in which folders that they should be placed in
When I navigate to 127.0.0.1:8000 I see
Using the URLconf defined in dcms.urls, Django tried these URL patterns, in this order:
^media/(?P<path>.*)$
^static\/(?P<path>.*)$
^en-us/
The current URL, , didn't match any of these.
My urls file is a copy/paste of what is in the tutorial I posted above
from django.conf.urls.defaults import *
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
Initially I thought maybe it is not calling the +u urlpatterns part of the code for whatever reason, but even when I delete the if statement and just have it call urlpatterns directly in the top part I still don't get the pretty splash page that should come up
any thoughts?

I wouldn't worry about i18 at this point.
Try using the regular patterns. Add this import:
from django.conf.urls import patterns
and replace i18n_patterns with regular old patterns.

Did you create a page?
Can you get to Admin? if so go into it and create a page at /

Related

File Browser no grapelli: NameError: name 'site' is not defined

I'm following this tutorial for install django-tinymce4-lite. At the end of the tutorial there are the indications to install django-filebrowser-no-grappelli.
I use Django 2.1.1 but even though I've followed all the indications, after the installation of the file browser was shown this message:
File
"/var/www/html/dev/miosito/django/beautifulsite_v0.1.1/djangosite/djangosite/urls.py",
line 25, in
path('admin/filebrowser/', include(site.urls)), NameError: name 'site' is not defined
Here there is urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from filebrowser.sites import site #sorry I've forgot this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('testapp.urls')), #app for my tests
path('tinymce/', include('tinymce.urls')),
path('admin/filebrowser/', include('site.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What I've wrong?
EDIT after Yeo correction:
I've add the string that I've forgot and I've correct
path('admin/filebrowser/', include(site.urls)),
with
path('admin/filebrowser/', include('site.urls')),
but now I've this new error:
ModuleNotFoundError: No module named 'site.urls'; 'site' is not a
package
Try the following: (remove the include)
# ...
from filebrowser.sites import site
# ...
urlpatterns = [
# ...
path('admin/filebrowser/', site.urls),
# ...
]
Always refer to the package official documentation, when you encounter errors specific to the package itself. (In this case is django-filebrowser, although the main repo seems to be at django-filebrowser-no-grappelli). Blog sometime gets outdated easily. For example the guide from your link does not specify what Django version they are using. (Looking from the way the tutorial was written include, it seems to be Django<1.9 (reference)).
If you're using Django>=2, then the official document should explain the correct way to install this package.

Why do my files not match a Django 1.8 installation even though Bash confirms I have one installed?

I set up the virtual environment variable for Django 1.8 as per the instructions in the Django tutorial (1.8) and the (almost) matching 1.7 tutorial on PythonAnywhere. When I go into Bash and follow the instructions to check the Django version it confirms that I have version 1.8. installed.
I am up to part 3 in the Django tutorial at this URL:
https://docs.djangoproject.com/en/1.8/intro/tutorial03/
In mysite/urls.py the tutorial tells me to write this:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
However, when I actually opened the file I was presented with this:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
The tutorial then has this to say:
Doesn’t match what you see? If you’re seeing admin.autodiscover()
before the definition of urlpatterns, you’re probably using a version
of Django that doesn’t match this tutorial version. You’ll want to
either switch to the older tutorial or the newer Django version.
As I said though, Bash confirms that I Do have Django 1.8 installed. What am I missing here? Why do I not have the correct files for Django 1.8 even though that's what I supposedly installed?
I tried to make the files match the tutorial files, but it only resulted in an error appearing on my public site.
The most likely cause is that you didn't activate the virtualenv when you ran startproject, so you got the default Django from PythonAnywhere instead of the one that you installed.

Page not found 404 on Django site?

I'm following the tutorial on Django's site to create a simple poll app. However, Django is unable to resolve "//127.0.0.1:8000/polls" , even though I've defined the regex in mySite/urls.py. I'm doing this in a virtualenv, with the latest Django (1.7) installed.
mySite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
mySite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mySite/polls/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
mySite/settings.py:
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
....
ROOT_URLCONF = 'mySite.urls'
The error I'm getting:
Using the URLconf defined in mySite.urls, Django tried these URL patterns, in this order: ^admin/
The current URL, polls, didn't match any of these.
I had the same problem.
It turns out I was confused because of the multiple directories named "mysite".
I wrongly created a urls.py file in the root "mysite" directory (which contains "manage.py"), then pasted in the code from the website.
To correct it I deleted this file, went into the mysite/mysite directory (which contains "settings.py"), modified the existing "urls.py" file, and replaced the code with the tutorial code.
In a nutshell, make sure your urls.py file is in the right directory.
Django unable to resolve 127.0.0.1:8000/polls because url config defined as r'^polls/'.
Usual workaround:
mySite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
Note:
Whenever Django encounters include(), It chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
mySite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('polls.views',
url(r'^$', 'index', name='index'),
)
Note: Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.
Answer If
If you want to access 127.0.0.1:8000/polls Note: without trailing slash
use view based url
url(r'^polls', 'polls.views.index', name='index'),
So now you can access 127.0.0.1:8000/polls without trailing slash.
You're accessing to http://yourdomain.com/, and you don't have any URL defined for "/".
You have two options:
If you want to access to the index page of your polls application you have to enter the URL: yourdomain.com/polls
You can also modify you mySite/urls.py file to access from just yourdomain.com
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('polls.urls')),
)
To make the answer clear for beginners who has this issue by following the tutorial, the project root URLconf is the one in the same folder as settings.py which is:
mysite/mysite/urls.py
Just make sure import 'include'. The code looks like:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
So in
mysite/mysite/settings.py:
The line should be:
ROOT_URLCONF = 'mysite.urls'
You don't need create a fresh new root URLconf.
Depending on where you put your ROOT urls.py, you set your ROOT_URLCONFIG accordingly, if you have it in your outermost folder containing manage.py then "urls" is ok. if you have it in someother folder then you have to do ".urls"
Credit for the answer to jerryh91
For more info about how it works, check How Django processes a request
You put the urls.py folder into the outer MySite folder, you are suppose to put it in the inner one so its not mySite/urls.py, but mySite/mySite/urls.py:
ran into the same mistake when i did the tutorial
Another way to access 127.0.0.1:8000/polls would be to redirect the browser when accessing 127.0.0.1:8000. It is done by editing .../mysite/mysite/urls.py as follows:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^$', views.index, name='index'),
]
Page not found?
If you get an error page here, check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/.
Source : https://docs.djangoproject.com/en/3.0/intro/tutorial01/
Actually the problem is that you didn't notice that
mysite/urls.py and polls/urls.py are two different files and you modified polls/urls.py instead of putting mysite/urls.py in the urls.py file in ...mysite\mysite folder.
In my case, it was a stupid mistake. I wanted to integrate the plugin django-tinymce, and test it. So following this guide, I did the step 3 and exported the variable to the path. As the server runned again, I received the not found error, showing the message:
Using the URLconf defined in testtinymce.urls, Django tried these URL
patterns, in this order: ....
But I didn't know what exactly it was, until I remembered exporting the variable DJANGO_SETTINGS_MODULE
running unset DJANGO_SETTINGS_MODULE in terminal solved my issue. Hope that it helps someone too.
Add the below line in your Mysite/urls.py
url(r'^$', views.index, name='index'),
and check. If you have created your project correctly, it should work. Else something like above might have happened to have more than one files so confused.
2017-10-05_12:03 ~/mysite/mysite
$ vi urls.py
2017-10-05_12:04 ~/mysite/mysite
$ cd ../..
2017-10-05_12:04 ~
$ mv mysite SENSIBLE_NAME_DJANGO_ROOT
i had the same issue and got it resolved by adding /polls after http://server:port/ and so final address in server looks like:
http://server:port/polls

Display static page in Django

I am trying to display contents of a static page in Django project.
urls.py :-
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'spollow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
url(r'^admin/', include(admin.site.urls)),
)
index.html is in the same directory as urls.py
I am getting 500 internal server error. Any ideas where I am going wrong?
First of all, what is the stacktrace from the 500 error saying that the error may be? You may be using Django 1.6 and the call to direct_to_template is deprecated.
On Django 1.5 or newer you can use TemplateView
Here's the example from the documentation
https://docs.djangoproject.com/en/dev/topics/class-based-views/
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
You can use the direct_to_template view on Django 1.4 or older
Here's the relevant documentation
https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)
If it is the latter, I would use a module instead of string, (look at the import on the example).
Other than that, without the 500 details it will be shooting in the dark, you may not have the right template, or an incorrect path, or a million different things.
Bonus note
If you just want to serve static pages, it might be better to serve them through the actual webserver in front of django (nginx, apache, etc), specially if you are expecting a high volume of traffic.
If Your error is due to unable to find index.html
if yours is an app(ie: created by python manage.py startapp <app>) then:
Then django will search for template files in <app>/templates directory, if you added the app to INSTALLED_APPS in settings.py.
so you need to create a folder templates inside your <app> and put index.html inside it.
if you don't have any apps, you want to add the template path manually :
open settings.py, then edit TEMPLATE_DIRS
TEMPLATE_DIRS = (
# Put the full path of the template dir here, like "/home/html/django_templates" or
# "C:/www/django/templates".
)
In Django 1.5 or newer you can use the render function instead of direct_to_template:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'django.shortcuts.render', {'template_name': 'index.html'}),
)
Or if you prefer the more complex way :), you can use class-based TemplateView:
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
)

No module named simple error in Django

I am working on a Django Bookmarks project and it requires you to call the simple.py from django.views.generic. But when I ran my server I got an import error that their was no module named simple. I looked in the folder and it was not there. I looked up some information on the issue. I read that in the newer version through git hub it does not have the file. I cannot figure out how to fix it. Any help would be much appreciated!
import os
from django.conf.urls import patterns, include, url
from django_bookmarks.bookmarks.views import *
from django.views.generic.simple import direct_to_template
site_media = os.path.join(
os.path.dirname(__file__), 'site_media'
)
urlpatterns = patterns('',
(r'^$', main_page),
(r'^user/(\w+)/$', user_page),
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^logout/$', logout_page),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': site_media}),
(r'^register/$', register_page),
(r'^register/success/$', direct_to_template,
{'template': 'registration/register_success.html'}),
# Examples:
# url(r'^$', 'django_bookmarks.views.home', name='home'),
# url(r'^django_bookmarks/', include('django_bookmarks.foo.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)),
)
It seems function based views have been deprecated in Django 1.3 (and may have been removed in the latest dev)
You should then replace them with the class-based views provided in Django 1.4
Deprecated function-based generic views
Class based views
You might be interested in the TemplateView.
django.views.generic.simple was deprecated and doesn't exist beyond django 1.4. Is there a specific reason you are using a development branch of django and not the latest stable version (1.4.2)?
I would recommend either using django 1.4.2 (which has django.views.generic.simple or use render (from django.shortcuts import render) instead of direct_to_template.