Django 1.5.2 Admin site no add or change buttons - python

I have a blog app inside of my own website app. The blog's objects (Posts, Comments) don't have the Add/Change buttons in the admin interface.
I used to have the admin code inside of my blog/models.py, but after reading this post I have moved the admin code in a separate file named blog/admin.py which looks like this:
from django.contrib import admin
from myapp.blog.models import Post, Comment
class PostAdmin(admin.ModelAdmin):
search_fields = ["title"]
admin.site.register(Post, PostAdmin)
class CommentAdmin(admin.ModelAdmin):
display_fields = ["post", "author", "created"]
admin.site.register(Comment, CommentAdmin)
However despite this change the add/edit buttons are still absent. I have admin.autodiscover() in my main app's urls.py. I also have admin as an installed app.

You should run below command after your changes
python manage.py syncdb
#then
python manage.py runserver
into your project root directory.
Edited
You must check your permission column into User section and make sure blog(add/edit/delete) option is given to that user .

Related

How to accept data in ModelForm and display data from DB in same webpage

I want to have a ModelForm that accepts user input and on the same webpage, also display all the records in the database. I created one view with a ModelForm called views.users and another view that displays all the users from the database called views.display_users. How do I put these both in my urls.py file? My urls.py code is below. Thank you!
# urls.py
from django.contrib import admin
from django.urls import path
from app3 import views
urlpatterns = [
path('users/',views.users,name='users'),
path('users/',views.display_users,name='display_users'),
]
You can integrate django-restframework link. This is one of the most popular django third party app to use in project. Django restframework provide viewset, where you can retrive, post, put, delete, list and also add many custom action in same end-point. Viewset tutorial link.

how to update django admin

I have an app named polls in Project A,and it's in admin too, but I added an app named pingpong later, I added the app pingpong in the installed_app and synced database(it works), but the admin page is still the same. So how do I do to make the admin page update?
You have to add your models in admin.py
from pingpong.models import YourModel
admin.site.register(YourModel)

Extending Customer's view from Django-Oscar

I am trying to override customer app in django-oscar. For that I have created customer app in my apps folder in project. While I run that project I encountered an error in django 1.7.4 as below:
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: customer
I went through the doc in django https://docs.djangoproject.com/en/1.7/ref/applications/#django.apps.AppConfig, But its not working out. So Is there any other way to extend any django-oscar's app and modify the code as per requirements.
This is my customer app's views.py:
from oscar.apps.customer.views import ProfileView as CoreProfileView
class ProfileView(CoreProfileView):
template_name = 'new_account.html'
and below is project's settings.py code snippet:
INSTALLED_APPS = [
'apps.customer',
]
Thanks in advance.
Run this command to overide apps from django oscar
./manage.py oscar_fork_app appname yourprojectname
yourprojectname-Your folder path to where the app should be created
Once you run this command a new app will be created with overided models,admin files.now add the app path inside
get_core_apps(['yourproject.order']) in settings.py file.
For more information please refer
http://django-oscar.readthedocs.org/en/latest/topics/customisation.html

admin.py for project, not app

How can I specify a project level admin.py?
I asked this question some time ago and was just awarded the Tumbleweed award because of the lack of activity on the question! >_<
Project:
settings.py
admin.py (This is what I am trying to get to work)
...
App
admin.py (I know how to do this)
For example, admin.autodiscover() is typically put in the project level urls.py (yes, it will be automatically included in 1.7)
I would like to move this, and the following, into their own admin.py file:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
UserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active')
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.autodiscover()
I tried doing just that, making an admin.py file and adding the code into it. Didn't work.
I tried adding a project level folder called admin, added an __init__.py and threw admin.py into the admin folder. Didn't work
Further I tried adding this admin folder to INSTALLED_APPS in settings.py. No luck.
From the admin.autodiscover doc string:
Auto-discover INSTALLED_APPS admin.py modules and fail silently when
not present. This forces an import on them to register any admin bits they
may want.
And here is the complete function very well documented:
def autodiscover():
"""
Auto-discover INSTALLED_APPS admin.py modules and fail silently when
not present. This forces an import on them to register any admin bits they
may want.
"""
import copy
from django.conf import settings
from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS:
mod = import_module(app)
# Attempt to import the app's admin module.
try:
before_import_registry = copy.copy(site._registry)
import_module('%s.admin' % app)
except:
# Reset the model registry to the state before the last import as
# this import will have to reoccur on the next request and this
# could raise NotRegistered and AlreadyRegistered exceptions
# (see #8245).
site._registry = before_import_registry
# Decide whether to bubble up this error. If the app just
# doesn't have an admin module, we can ignore the error
# attempting to import it, otherwise we want it to bubble up.
if module_has_submodule(mod, 'admin'):
raise
So the only thing autodiscover does for you is look for some module called admin.py inside installed app directories, hence you can put your admin.py where you want just make sure you import it, this make the code in it (registration of models ect..) get executed.
IMPORTANT: I'm not sure the correct moment for importing your custom-path admin.py. But it's sure you have to import it after load all the related apps.
All admin.autodiscover() is import all the admin files it finds. There is no point putting the line itself in an admin files.
To understand why it's not working, you need to realise how it works. What it does is import the admin.py files inside registered apps. A registered app is a package included in INSTALLED_APPS that has a models.py. That's worth repeating: an app isn't considered if it doesn't have a models.py - even a blank one will work, but without it the package isn't an app.
So if you create a blank models.py in your directory, ensure that that directory is in INSTALLED_APPS, and move admin.autodisover back to urls.py, everything will work.
Edit
I'm not exactly sure what you do want, and why. As I mentioned, autodiscover depends on apps; but as I also mentioned, all autodiscover does is import all the admin files. If you really don't want to follow best practices, all you need to do is import your admin file manually: you can do that in the same place that you normally call autodiscover from, ie in the main urls.py.
Sooner or later, a heavily customized admin class will most likely need a project wide admin.py, as it is the place to register all per-app admins that are not being auto-discovered anymore. The documentation mentions this, but there is no extensive example to illustrate it:
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#customizing-the-adminsite-class
Note that you may not want autodiscovery of admin modules when using your own AdminSite instance since you will likely be importing all the per-app admin modules in your myproject.admin module. This means you need to put 'django.contrib.admin.apps.SimpleAdminConfig' instead of 'django.contrib.admin' in your INSTALLED_APPS setting.
There is a bit outdated but more or less valid example in the wiki as well for multiple admin sites:
https://code.djangoproject.com/wiki/NewformsAdminBranch
# project-level admin.py
from django.contrib import admin
from myproject.myapp.models import Book, Author
from myproject.anotherapp.models import Musician, Instrument
site1 = admin.AdminSite()
site1.register(Book)
site1.register(Author)
site2 = admin.AdminSite()
site2.register(Musician)
site2.register(Instrument)
I'm curious to see if this gets downvoted. It seems to work, but it feels wrong. ;-)
The question is how to override the UserAdmin settings.
Put an admin.py in your project folder (alongside settings.py)
from django.contrib.auth import admin
from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display = ('email', 'first_name', 'last_name',
'is_active', 'date_joined', 'last_login',
'is_staff')
UserAdmin.ordering = ['-date_joined']
add your project to installed_apps (this seems wrong, but I don't know why)
And now you should see the new columns and sort order on /admin/auth/user/
This is working for me with Django 1.11. So far no issues that I can find. What say you StackOverflow?

Django admin.site.register doesn't add my app admin

as a django newbie (I have some exprience with other python webframework like turbogears and bottle but exploring django) I'm trying to auto create the admin management for my app model
in tha main URLS.py I have:
edit:
from django.contrib import admin
admin.autodiscover()
and after that:
urlpatterns = patterns('',
url(r'^appname/',include('appname.urls')),
url(r'^admin/',include(admin.site.urls))
notice this is in the main urls.py and not in the app urls.py
following the tutorial (which did work for me in the tutorial..) I created an 'admin.py' file in the appname folder and there:
from appname.models import Appname
from django.contrib import admin
class appnameAdmin(admin.ModelAdmin):
fieldsets = [various field sets and fields etc ]
admin.site.register(Appname,AppnameAdmin)
and in setting.py I have uncommented
'django.contrib.admin'
I don't get any error in the commandline window and the basic admin screen does appear (auth and sites)
I checked the imports in admin.py in the manage.py shell and everything seemed to work allright, I also tried commenting AppnameAdmin class out and registring just:
admin.site.register(Appname)
but that didn't work eith
I'm guessing I'm missing something obvious - I'll be glad to help with that
using django 1.4 + python 2.72
Check all of these:
There are seven steps in activating the Django admin site:
Add 'django.contrib.admin' to your INSTALLED_APPS setting.
The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and
django.contrib.sessions. If these applications are not in your
INSTALLED_APPS list, add them.
Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to
MIDDLEWARE_CLASSES. (These are both active by default, so you only
need to do this if you’ve manually tweaked the settings.)
Determine which of your application’s models should be editable in the admin interface.
For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for
that particular model.
Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.
Hook the AdminSite instance into your URLconf.
Do you have all the other admin dependencies in your installed apps?
Do you have admin.autodiscover() in your URLS.py?
Also, I think your code should look something more like this:
from projectname.appname.models import Appname
from django.contrib import admin
class AppnameAdmin(admin.ModelAdmin):
fieldsets = [various field sets and fields etc ]
admin.site.register(Appname,AppnameAdmin)
Have you restarted the server process?
Maybe this helps someone: in my case the problem was solved by stopping and starting the server process, because when you add a new admin.py file it does not reload automatically.
aaargghhh - I found the problem. I saved admin.py in the template/appname/ folder instead of the appname/ folder. so stupid of me. so sorry for the interruption.
Set this in your model admin:
def has_add_permission(self, request, obj=None):
return True
def has_change_permission(self, request, obj=None):
return True
def has_delete_permission(self, request, obj=None):
return True
Check all these:
Restart the server and check again
Add 'models.Model' as a parameter in your class in models.py file
class Classname(models.Model):
Add your app in the 'INSTALLED_APPS' in settings.py, In my case it's 'travello.apps.TravelloConfig'
INSTALLED_APPS = [
'travello.apps.TravelloConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Add 'admin.autodiscover()' in main urls.py
from django.contrib import admin
from django.urls import path, include
admin.autodiscover()
urlpatterns = [
path('', include('travello.urls')),
path('admin/', admin.site.urls),
]
it worked for me!
Nothing above worked for me.
Then I went to Settings, and under Project:MyApp Project Interpreter
I switched the Project Interpreter from Python 3.8(venv) to Python 3.8(MyApp)
And then all my models where registered (could list them in http://localhost:8000/admin/)
Strangely enough, still in admin.py, after "admin.site." the method registered will not be listed as available. But it works anyway.
Go to models.py file and add 'models.Model' as a parameter in your class .
Example:
class className(models.Model)
In your case use below as class name and it will 100% work for you.
class AppnameAdmin(models.Model):
from django.contrib import admin
from .models import modelname
admin.site.register(modelname)
import models in this way
Please consider that some models can be seen only from a superuser.
Try to create one and log-in the admin with that user.
python3 manage.py createsuperuser

Categories