App won't show up in django admin - django

I've just installed django on my mac os x snow leopard and got some issues with it.
I just made a very simple project that only contains a simple app.
The app contains just one model and it's a task. When running syncdb the table for tasks is created without any problems and I'm requested to create new user. Everything works fine and I can log in and so on but my app is not showing up.
Here is some code from my project.
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'work.todo',
)
todo/admin.py
from work.todo import Task
from django.contrib import admin
admin.site.register(Task)
urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^work/', include('work.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS 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)),
)
todo/models.py
from django.db import models
class Task(models.Model):
text = models.CharField(max_length=200)
done = models.BooleanField()
Maybe it's worth to mention that there is a __init__.py in both work and the work/todo folder.
Cheers,
nandarya

In todo/admin.py:
from work.todo.models import Task
from django.contrib import admin
admin.site.register(Task)
You forgot models in your import statement :)

Related

How can i fix Page not found 404 in django?

Hi ive been coding my first website and then try running the product page I get this error
*Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products
Using the URLconf defined in myshop.urls, Django tried these URL patterns, in this order:
admin/
The current path, products, 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.*
how can I solve this? Here is my code...
my views page code
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello world')
def new(request):
return HttpResponse('New Products')
productsurls code
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('new', views.new)
]
myshopurls
from django.contrib import admin
from django.urls import path, include
urlpatterns = {
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
}
Most probably, you are facing this problem because you have not include your app "products" in the "settings.py" file.
-> After checking your code I can conclude that"myshop" is your project and "products" is your app.
So, you will have to go in settings.py file under which you will find a list "INSTALLED_APPS".
Inside INSTALLED_APPS -> You will have to include your app.
Go to "apps.py" file of the "products" app and copy the name of the "config" class.
After that in your settings.py file, you will have to write 'products.apps.(paste the name of config class)'.
Most probably name of your config class will be "ProductsConfig" -> So, you will have to write 'products.apps.ProductsConfig',
Hope it will work !
You have to include the app under INSTALLED_APPS in settings.py file.
So your settings.py file should read like so
INSTALLED_APPS = [
# Built In Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# User Defined Apps
'product.apps.ProductConfig',
]
Tip - If you are confused as to how to include the name under installed Apps. It should be like so
<myapp>.apps.<name_of_class_in_myapp/apps.py_file>
Basically navigate to your app folder -> apps.py file and check the name of the class. It will be like so
from django.apps import AppConfig
class ProductConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'product'
You want the name of this class. ProductConfig.
Note - Local apps should always be added at the bottom because Django executes the
INSTALLED_APPS setting from top to bottom. We want the core Django apps to be available before our app.
The reason for listing the app like
'product.apps.ProductConfig',
is because it is best practice and helps if in future if you decide to use Django Signals.

how to overcome the following url issue?

Hello I am beginner learning Django I am triying to follow this tutorial:
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
I created an app called Polls to test my site:
Since I dont have idea where to put the file called urls.py
I put this file at the following directories:
Django/mysite/polls/urls.py
Django/mysite/polls/migrations/urls.py
This file contains:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And finally I added the following lines:
at this level:
Django/mysite/mysite/urls.py
this file contains:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
I dont know where is the issue but when I run:
Django/mysite$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 21, 2017 - 15:59:43
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I got in the page the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , 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.
So I really appreciate support to overcome this issue,
thanks for the support
the urls.py doesn't go into the migrations folder.
Did you register your Polls app in the settings.py file? you need to add the name of the Polls app to the INSTALLED_APPS list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'example: add app name here',
'polls',]
you then need to create a url.py file within your polls app.
polls/
urls.py
In that file you would add:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
the url you want to use in your browser would be something like this:
http://127.0.0.1:8000/polls

how to override or substitute default django admin site template?

FIRST:
I used django admin site as my website management system,but some features are not appropriate to me,so i decide to make my own,according to django docs,what i learned is that I can override Templates per app or model while i can extend AdminSite to build a brand new one.But,i have some problems playing around them,what i was trying to do were:
1,article/admin.py (one app of my project)
from django.contrib import admin
from django.contrib.admin import AdminSite
from .models import Article,ArticleImage,Category
class MyAdminSite(AdminSite):
site_header = "Moonmoonbird administration"
admin_site = MyAdminSite(name="myadmin")
admin_site.register(Article)
2,settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_swagger',
#'django.contrib.admin',
'django.contrib.admin.apps.SimpleAdminConfig',(which docs suggest to do)
'jwtauth',
'navigation',
'myadmin',
'article',
)
3,urls.py
from django.conf.urls import patterns, include, url
from jwtauth.views import obtain_jwt_token
from django.contrib.auth.admin import admin
from article.admin import admin_site
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'moonmoonbird.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'admin/',include(admin.site.urls)),
url(r'myadmin/',include(admin_site.urls)),
url(r'^auth/',obtain_jwt_token.as_view())
)
When i access example.com/admin and example.com/myadmin,they are the same,both are the django admin site default implementation.I cannot see where i was doing wrong.
SECOND:
Lets say a use case(which i meet now),an article app of my project,which has title,tag,and content attributes,I install it into admin site by doing this:
article/admin.py:
from django.contrib import admin
admin.site.register(Article)
admin.site.register(Category)
admin.site.register(ArticleImage)
and now i can manipulate CURD in admin site,but what i want is that i can edit my article content like SO do,that is using a pagedown editor for say(which actually i want to use) ,but i dont know how to do it.
Question:
1, how can i make the FIRST workflow works?
2,how to do and what you suggest me to do to implement the SECOND case?
Thanks in advance!
Think well if you really have to change the whole admin template. In majority of cases, you need several small changes. There are good properties in admin.ModelAdmin class for many useful customizations. It is even possible to override some parts of the admin template with your HTML template.
Read the Django tutorial, Part 2 for examples how to easily change Admin page functionality and look.
Can you tell what exactly you need to change in your admin page?

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')

Moving to production environment broke my application

I've just deployed my application on dreamhost and it's stopped working when uploading files. Passenger just produces a 500 internal error. It works on my development set up.
My passenger file looks like:
import sys, os
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), '/photosoc'))
os.environ['DJANGO_SETTINGS_MODULE'] = "photosoc.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I get the standard 404 error(debug is still enabled). I have had to change my urls file though from:
from django.conf.urls.defaults import patterns, include, url
from competition.models import Image
from competition import *
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
admin.site.register(Image)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photosoc.views.home', name='home'),
# url(r'^photosoc/', include('photosoc.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'^image/$', 'competition.views.index'),
url(r'^image/uploadImage', 'competition.views.uploadImage'),
url(r'^image/uploadCompleted', 'competition.views.uploadCompleted'),
url(r'^image/uploadFailed', 'competition.views.uploadFailed'),
)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
To the following:
from django.conf.urls.defaults import patterns, include, url
from competition.models import Image
from competition 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()
admin.site.register(Image)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photosoc.views.home', name='home'),
# url(r'^photosoc/', include('photosoc.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'^image/$', 'competition.views.index'),
url(r'^image/uploadImage', 'competition.views.uploadImage'),
url(r'^image/uploadCompleted', 'competition.views.uploadCompleted'),
url(r'^image/uploadFailed', 'competition.views.uploadFailed'),
)
This is the only change I've made to the application from the switch from development to production. So what else could have gone wrong?
You may have a permission problem.
It seems like you are writing image files. Does the process that is running Django have permission to write files? When you are running Django locally with ./manage.py runserver Django will get the permissions of the user that runs from the command line, but in a typical production environment you'd be running with the permissions of the Apache user. This user may not have permission to write those image files. You could try temporarily hardcoding the write directory to /tmp to see if that makes a difference.
Of course, you will really need see that 500 error stack trace to know what's going on.
Are your settings exactly the same in what middleware classes your importing? Double check that these are the same.
Related:
ModSecurity: Output filter: Failed to read bucket (rc 104): Connection reset by peer