django cms apphook error - django

I am learning django-cms. I tried to make custom plugin which was quite successful but when I tried to hook my custom made plugin to apphook, its giving me an error, saying,
No Module named urls
.
I followed the tutorial which was given in django cms sites documentation, and created the cms_app.py file. Currently my application directory has all the files which is required to make a custom plugin for django cms, and an additional file of cms_app.py.
Is something wrong with setting of the url or do I need to create a new urls.py file inside my app directory?
My cms_app.py is exactly the same as given in the tutorial.
i have created a project called myproject using command -
python django-admin.py startproject
myproject
After referring to the tutorial given for cms I created a plugin called first, using the basic command
python manage.py startapp first
Now the plugin is working perfectly well, and the directory structure before making an attempt to the apphook was,
first/
__init__.py
cms_plugins.py
models.py
tests.py
views.py
Now after making an attempt to hook the app in apphook, the directory structure is:
first/
__init__.py
cms_app.py
cms_plugins.py
models.py
tests.py
views.py
My cms_app.py is as follows:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
class FirstApp(CMSApp):
name = _("First App") # give your app a name, this is required
urls = ["first.urls"] # link your app to url configuration(s)
apphook_pool.register(FirstApp) # register your app
i have a urls.py file in myproject folder, and it is as follows:
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.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)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
I have restarted the server as was mentioned in the tutorial, but no success.
Any ideas as to what is wrong with my simple app?!
EDIT - 1
My views file is as follows:
from django.http import HttpResponse
def index(request):
“””Generate the context for the main summary page”””
return render_to_response(‘first/first.html’)
Edit - 2
I have changed my urls.py inside first app folder to this :
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.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)),
url(r'^first/$', include('first.views.index')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
But now I am getting this error:
SyntaxError at /
Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.3
Exception Type: SyntaxError
Exception Value:
Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)
Exception Location: /usr/local/lib/python2.6/dist-packages/django/utils/importlib.py in import_module, line 35
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 11:00:41 -0500
I have edited the urls and views but now I am getting this error.
NameError at /first/
global name 'render_to_response' is not defined
Request Method: GET
Request URL: http://localhost:8000/first/?preview
Django Version: 1.3
Exception Type: NameError
Exception Value:
global name 'render_to_response' is not defined
Exception Location: /home/naveen/django_projects/myproject/first/views.py in index, line 5
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 14:50:32 -0500

You do not have a first.urls module with the URLs of your 'first' app. Next to your file first/models.py, create a file first/urls.py which contains the URL patterns for the 'first' app.
For the views you give in your questions, the urls.py should look something like this:
from django.conf.urls.defaults import *
from first.views import index
urlpatterns = patterns('',
url(r'^$', index),
)
Also note that in your views, you use non-standard quote characters, it should look like this:
from django.http import HttpResponse
def index(request):
"""Generate the context for the main summary page"""
return render_to_response("first/first.html")

Related

Page not found error - Writing your first Django app, part 1

I have installed Django 2.2.5 in my conda environment and I'm following the Django tutorial for Writing your first Django app, part 1
I followed the steps exactly but I'm getting a Page Not Found error when I try to access the polls/ url.
My root directory structure is like this
mysite/
manage.py
db.sqlite3
urls.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
And my view is like this
D:\TEMP\djangotest\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")
My two urls files are like this
D:\TEMP\djangotest\mysite\polls\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
D:\TEMP\djangotest\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),
]
And I run the app from the environment like this
(py3.6) D:\TEMP\djangotest\mysite>python manage.py runserver
But when I go to the url indicated in the tutorial it give the 404 error - page not found from the console
October 21, 2019 - 16:23:13
Django version 2.2.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /polls
[21/Oct/2019 16:23:19] "GET /polls HTTP/1.1" 404 1954
From the browser it look like
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.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Your main urls file is in the wrong directory. It looks like you've created a separate urls.py in the base "mysite" directory; instead you should have edited the existing one in the "mysite/mysite" directory. The one you've created isn't being used at all.

NoReverseMatch " " is not a registered namespace

Hello guys I've been struggling to move my project in another location in anaconda so after finally installing everything and setting up the project I am getting some errors that I don't understand. First of all I had my code on a sub-folder inside my app called api there I had my views, serializers, urls. And I included the urls but nothing seemed to happen. I moved all the api files to the app folder and I deleted the api folder. Now I'm getting this error NoReverseMatch at /op_data/objects/ ('api-op-data' is not a registered namespace). Even after deleting this url I keep getting the same error. This is my code:
urls.py
from django.urls import path, re_path
from django.views.generic import TemplateView
from django.conf.urls import url, include
from django.contrib import admin
from djgeojson import views
from djgeojson.views import GeoJSONLayerView
from django.conf.urls.static import static
import MMA
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from MMA import views
from rest_framework_jwt import views
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/auth/token/$', obtain_jwt_token, name='api-auth-token'),
url(r'^api/', include(('MMA.urls', 'api-op-data'), namespace='api-op-data')),
]
urls.py
from django.conf.urls import url
from django.contrib import admin
from .views import OP_Data_RudView, OP_Data_ApiView, UserCreateAPIView, UserLoginAPIView, WoType_ApiView, WoType_RudView, UserObjects_ApiView, UserObjects_RudView
app_name = 'MMA'
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/register/$', UserCreateAPIView.as_view(), name='register'),
url(r'^users/login/$', UserLoginAPIView.as_view(), name='login'),
url(r'^op_data/$', OP_Data_ApiView.as_view(), name='post-listcreate'),
url(r'^op_data/(?P<pk>\d+)/$', OP_Data_RudView.as_view(), name='post-rud'),
url(r'^op_data/wo_type/$', WoType_ApiView.as_view(), name='post-listcreate'),
url(r'^op_data/wo_type/(?P<pk>\d+)/$', WoType_RudView.as_view(), name='post-rud'),
url(r'^op_data/objects/$', UserObjects_ApiView.as_view(), name='post-listcreate'),
url(r'^op_data/objects/(?P<pk>\d+)/$', UserObjects_RudView.as_view(), name='post-rud'),
]
error log:
NoReverseMatch at /op_data/objects/
'api-op-data' is not a registered namespace
Request Method: GET
Request URL: http://---.--.-.---:7000/op_data/objects/
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:
'api-op-data' is not a registered namespace
Exception Location: C:\Users\Administrator.HR-JUGOR\Anaconda3\envs\MMA\lib\site-packages\django\urls\base.py in reverse, line 86
Python Executable: C:\Users\Administrator.HR-JUGOR\Anaconda3\envs\MMA\python.exe
Python Version: 3.6.5
Python Path:
['C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\Mobile',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\python36.zip',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\DLLs',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages\\win32',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages\\win32\\lib',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages\\Pythonwin']
Server time: Wed, 24 Jul 2019 09:49:27 +0000
/op_data/objects/ isn't the configured url for post-listcreate, its api/op_data/objects/.
In this case the namespace argument seems to be redundant, you already declared the namespace as '^api/' before the include(), declaring it explicitly as 'api-op-data' later the same line seems unnecessary based on the code provided, though I would need to see how you were accessing these urls from your views and templates to know for sure.
You also don't need to redeclare your admin url in MMA.urls, and I question your decision to include the api/auth/token url in the root url config, it seems like it belongs in MMA.urls with the other urls in the api namespace

The empty path didn't match any of these. (Python3, Django, URLS)

I'm learning Python3. I installed python and django on my webserver.
But I have a problem with URLs.
Here's what I tried :
sctructure:
expnk.ru
HelloDjango (1)
HelloDjango (2)
setting.py
..........
urls.py
cat
...
any apps
run "cat" from ssh command:
python3 manage.py startapp cat
next im going to HelloDjango (2) file and edited file setting.py, for INSTALLED_APPS and added 'cat'
then I'm doing HelloDjango (2) -> urls.py. I added to this file the next strings:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^cat/', include('cat.urls')),
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls')),
url(r'^test/', include('test.urls')),
]
then I do: cat -> urls.py and add to this file:
from django.conf.urls import url, include
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
lastly, I edited file views, code from this file is below:
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse("Hello world!")
# Create your views here.
I created the cat application similar to the blog application, as a result: the blog is running, but cat is not.

grappelli dashboard ValueError on two admin sites

I want use two admin sites for my project. Each with grappelli dashboard. I've executed this commands:
python manage.py customdashboard dashboard.py
python manage.py customdashboard dashboard.py
twice (once in project/project and second time in project/app)
#file system
project
project
dashboard.py
urls.py
app
dashboard.py
admin.py
#settings.py
GRAPPELLI_INDEX_DASHBOARD = {
'django.contrib.admin.site': 'project.dashboard.CustomIndexDashboard',
'app.admin.operator_site': 'app.dashboard.CustomIndexDashboard',
}
#urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
from app.admin import admin_site
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^myadmin/', include(operator_site.urls)),
url(r'^grappelli/', include('grappelli.urls')),
)
#app/admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
pass
admin_site = MyAdminSite()
The problem is when I go to /admin/ everything is ok, but when I go to /myadmin/ , I've got ValueError
Dashboard matching "{'app.admin.operator_site': 'app.dashboard.CustomIndexDashboard', 'django.contrib.admin.site': 'project.dashboard.CustomIndexDashboard'}" not found
full error trace: http://pastebin.com/w8W2eRPd
Where is the problem?
Ok, i've found it out. When making a subclass of a AdminSite on make an instance
admin_site = MyAdminSite()
you should use a custom name parameter (not 'admin'):
admin_site = MyAdminSite(name='myadmin')

Django Template Issue

I am following along on this tutorial, after I add the templates and put the pattern in urls.py, I go the website I get a template not loaded page.
This is my template dirs section from settings.py:
TEMPLATE_DIRS = (
"/home/django-projects/base/templates/",
)
the base folder hierarchy is this:
blog<dir>
dbs<dir>
__init__.py
manage.py
settings.py
templates<dir>
urls.py
urls.py looks like this:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('base.blog.views',
(r"", "main"),
# Example:
# (r'^base/', include('base.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
In templates I have a folder called blog, and it contains the two html files from the tutorial. I am just not sure what setting I am missing to display the proper templates. I will be happy to post anything else you would need to see. I am sure it is something trivial, I just can't seem to figure out what it is. Thanks for your help.
EDIT
So I was playing around with it, and I realized I couldn't get to admin. So I uncoupled the urls for the blog part away from the main. The urls.py on the top level looks like this:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'blog/', include('blog.urls')),
# Example:
# (r'^base/', include('base.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
And in the blog folder urls.py looks like this:
from django.conf.urls.defaults import *
urlpatterns = patterns('base.blog.views',
(r'^$', 'main'),
)
Now I can get to admin, but still get a template load error when I try to get to the blog.
TemplateDoesNotExist at /blog/
blog/list.html
Request Method: GET
Request URL: http://192.168.1.124:9999/blog/
Django Version: 1.2.3
Exception Type: TemplateDoesNotExist
Exception Value:
blog/list.html
Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg/django/template/loader.py in find_template, line 138
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path: ['/home/kevin/django-projects/base', '/usr/local/lib/python2.6/dist-packages/setuptools-0.6c11-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/BeautifulSoup-3.1.0.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Tue, 2 Nov 2010 11:59:13 -0500
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
Should the last line of the "main" view contain "blog/list.html" instead of "list.html"?
Try replacing that line with the following:
return render_to_response("blog/list.html", dict(posts=posts, user=request.user))
Do the same thing with the "post" view.
Basically, Django is going to look under each of the configured template directories in settings.py and if you specify "list.html" it better be in the root of of of those directories. Since you put the "list.html" in the "blog" sub-directory and not where you told it to look, Django can't find it.
First, you have a missing leading / in your template directory:
TEMPLATE_DIRS = (
"/home/django-projects/base/templates/",
)