When I add an app with the name advertisements (add models, admin + register in INSTALLED_APPS) it is not listed in the admin interface (it is very hard to see but it disappears after microseconds).
models.py:
from django.db import models
# Create your models here.
class Advertisement(models.Model):
pass
admin.py:
from django.contrib import admin
from .models import Advertisement
# Register your models here.
#admin.register(Advertisement)
class AdvertisementAdmin(admin.ModelAdmin):
pass
apps.py:
from django.apps import AppConfig
class AdvertisementsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'advertisements'
settings.py:
INSTALLED_APPS = [
# ...
"advertisements",
]
Changing an app directory to something like advertisements2, app name (in apps.py) to advertisements2 solves the problem.
Why the app with the name 'advertisements' is not listed in the admin interface?" What is the issue with the name "advertisements"?
I tested in many times in completely blank project.
The question seemd to be a bit strange for me at the beginning and I was feeling super curious about what is going on. I recreated your code perfectly and I was getting the same result as you. No advertisemets app in admin panel. I started a debugger and tracked execution line by line, and everything seemd to be in order. But after some time it occured to me, that it could not be actually django's fault. And it is not! Beware the most intriguing solution to your problem:
Just disable your adblocker and refresh the site!
What is most problably the problem (and what was the problem on my site) is that my adblocker blocked the word advertisement and all the styles that came with it. So with your adblocker turned on, if you were to inspect a site you would see your advertisements app sitting comfortably in your html code:
<div class="app-advertisements module">
<table>
<caption>
Advertisements
</caption>
<tbody><tr class="model-advertisement">
<th scope="row">Advertisements</th>
If you were to remove app-advertisements class from <div class="app-advertisements module"> (again, with the adblocker still turned on) the Advertisement should magically apear in admin panel.
Try using admin.site.register(Advertisement) instead of the AdminView in admin.py. If this line works, you could add the model admin view individually like this:
class AdvertisementAdmin(admin.ModelAdmin):
pass
admin.site.register(Advertisement, AdvertisementAdmin)
Related
Using django 4.0.3 and the restframework.
I have serval models registered in django admin:
#admin.register(models.Drug)
class DrugAdmin(admin.ModelAdmin):
list_display = ('apl_label', 'name', 'drug_class', 'atc_code', )
#admin.register(models.Organism)
class OrganismAdmin(admin.ModelAdmin):
list_display = ('apl_label', 'long_name', 'short_name', 'genus', 'flagged', 'notes')
....
For some reason, some o fthe models are not showing up in the admin view. In this example, Organism shows up, but Drug is hidden. I can go to http://localhost:8000/admin/api/drug/ and access this admin page, but it is not listed under http://localhost:8000/admin/. There is no error message either. I did run makemigrations and migrate.
Permissions are only set globally in settings.py:
...
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
],
It is always the same models not showing up. I tried deleting the database and creating everything from scratch. No luck. Why does Django decide to not show certain models?
But when I go to http://localhost:8000/admin/api/organism/ all models show up in the side bar:
I tried registering the admin page with admin.site.register(Drug), but that did not change anything.
If this solves your problem you can use this. Or you can check this to understand is the problem because of your admin.py or not.
This will work properly:
from django.contrib import admin
from .models import (
Drug,
Organism,
)
admin.site.register(Drug)
admin.site.register(Organism)
That should not happen. Your code seems perfectly fine to me. Make sure your top imports contain the following lines too:-
from django.contrib import admin
from . import models
If your models don't have any extra customization like custom fieldsets and/or custom section-titles, you can try using the regular old admin.site.register(models.modelname). Maybe the decorator is malfunctioning for you for some reason, though I have never seen that happen before.
I am unable to add my sub apps like category, user in settings.py. It was working fine but don't know what mistake I have done. Please help. I'm not able to progress from here.
Please create a model and get that model to admin panel.
Link to github
in the admin.py within your "api" folder register the created models so as to display it in the admin panel,
api: admin.py
from django.contrib import admin
from .models import (model created in the models.py)
# Register your models here.
admin.site.register(model created in the models.py)
link to official documentation :
Admin site Official docs
I'm hosting a Django site through mod_wsgi and WAMP with Python 2.7. On my admin site the Users, Groups and Sites sections all have Add and Change buttons. While there is a section each for both of my own custom models, Feedpost and Newspost, they have no buttons at all and there is no way to add or change them.
I believe this change came about when I switched from using the Django internal testing server to using WAMP. Does anyone know what's causing this and how to get my Add and Change buttons back?
EDIT:
Here's one of the two models:
from django.db import models
from django.contrib import admin
class FeedPost(models.Model):
title = models.CharField(max_length=50)
text = models.CharField(max_length=5000)
date = models.DateTimeField('Date Published')
def __unicode__(self):
return self.title
admin.site.register(FeedPost)
The other is nearly exactly the same.
It turns out that using the Django development server localhost:8000/admin insists on going to my site's homepage and I can't access the admin site at all.
Also I think this is going to turn out to be the problem because I haven't got an admin.py file anywhere in my project...
These need to be in the admin.py file for starters.
from django.contrib import admin
admin.site.register(FeedPost)
I want to have 2 separate admin sites inside a Django project.
By separate I mean - they should have separate users authentication, they should administer different models, and have different looks and URLs.
The reason I want to do it is the customer wants separate section to administer the CMS part of the page, and separate to use as a 'back-office' solution.
I thought about just making a copy od django.contrib.auth appliaction in my project tree, naming it differently and using separate admin.site.register() calls for both of them. This way I can have other models available in each one of them, diffrent looks, etc. I don't know how to solve the user-authentication problem (I should have different user to be able to log into CMS then into the BackOffice).
Anyone happened to do this before and could give me some hint? Or what I plan to do is just wrong by design?
You can subclass Django's AdminSite (put it eg. in admin.py in your project root):
from django.contrib.admin.sites import AdminSite
class MyAdminSite(AdminSite):
pass
#or overwrite some methods for different functionality
myadmin = MyAdminSite(name="myadmin")
At least from 1.9 on you need to add the name parameter to make it work properly. This is used to create the revers urls so the name has to be the one from the urls.py.
Then you can use it in your app's admin.py the same way as you do with the normal AdminSite instance:
from myproject.admin import myadmin
myadmin.register(MyModel_A)
You also need to define some urls for it (in your project's urls.py):
from myproject.admin import admin, user_site
from myproject.admin import myadmin
urlpatterns = patterns('',
...
(r'^admin/', include(admin.site.urls)),
(r'^myadmin/', include(myadmin.urls)),
Also see this: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adminsite-objects
To register models in different AdminSites you just need to create different instances of django.contrib.admin.sites.AdminSite, see this.
You will be good to go with two different admin sites managing different models and having different templates.
For authentication and permissions you should be able to use the build-in django.contrib.auth as is with custom permissions (hope someone else will be able to help more here)
I'm not sure that my finding, reported here, would have been entirely helpful to kender because, among other things, I don't know if he was talking not only about two admin sites but also two databases, one for each. That's my situation. I got the bright idea that I wanted one of my apps, a new app, to have its own database and own admin pages.
But I ran into a problem with the AdminSite subclassing approach of Bernhard Vallant, though it seems to be the orthodox and essentially correct thing to do. I resolved the problem.
Here's the mod to Bernhard Vallant's code that I found to be utterly necessary:
from django.contrib.admin.sites import AdminSite
class MyAdminSite(AdminSite):
pass
#or overwrite some methods for different functionality
myadmin = MyAdminSite(name='anything')
Yes, I do really mean name='anything' that you choose (as long as it isn't 'admin'). And I've toggled in and out with it and it fails every time without the anything-but-admin name assignment.
The symptoms that I acquired were that when I added the second database and created a myadmin for it and then registered the model with myadmin.register(My_ModelA), and went to look at the two admin app pages, the one for my new app that used the second database and myadmin and the model My_ModelA looked fine, but my old admin page showed dead links for its models and when I clicked there on a non-dead link for an app (an old app that uses the old database) I got a 404 code to the effect that the page didn't exist.
Also, I don't know that it matters, but I did something different from what
Bernhard Vallant did in the project urlconf:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include('mynewapp.urls')),
url(r'^someword/admin/', include(admin.site.urls)),
)
OK, "someword" is irrelevant--- there for appearances with regard to the end user and not the name of an app or the project. But the associated admin is the one for my old app and old database. Note the autodiscover() inclusion. There's some murky language in the docs to which Bernhard Vallant linked regarding its use when the project urlconf is configured as Bernhard Vallant has it with the myadmin import but also with a reference to the default admin.
And for the urlconf for mynewapp I have:
from django.conf.urls import patterns, url, include
from myproject.admin import myadmin
urlpatterns = patterns('',
url(r'/', include(myadmin.urls) )
)
urlpatterns += patterns('mynewapp.views',"... url() stuff for mynewapp's views"),
)
Notwithstanding the utter necessity of naming your AdminSite instance internally to something other than 'admin', I must add that when it came time to jazz up the mynewapp's admin.py file with some admin.ModelAdmin subclassing, it was necessary to indeed use admin.ModelAdmin as the parent class. myadmin is after all an instance of a subclass of AdminSite. As such I gather that it's on a par with admin.site, not with admin.
This is all very confusing to a NOOB like me because admin, with the lower case, looks like an instance, and I am unfamiliar with subclassing instances. So I assume that it isn't.
I am implementing the stock Django comments to an existing site.
I'd like comments to appear in multiple apps and models and have all the comments behave the same - i.e. an email sent, plus other bits (listening to 'flag' signals and dealing with accordingly)
Where's the best place to put my custom moderator code?
I understand that I can pass in an iterator of Models to the register function - at first I placed it inside the __init__.py module of my main app as so:
from django.contrib.comments.moderation import moderator, CommentModerator
from app.models import Model1
from app2.models import Model2
#.... etc
class MyCommentModerator(CommentModerator):
email_notification = True
enable_field = 'enable_comments'
#...
moderator.register(
[Model1,Model2,Model3,Model4,...],
MyCommentModerator
)
But this gave an error saying that Model1 was already registered.
I would probably re-factor this code into a comments_moderation.py module - but where should I include it?
Or is it best practice to register each model inside each apps models.py file?
Are there any samples out there that use comments?
I only found out how the Comment moderation queue works by trial and error - are there any docs for this that I've missed?
Comment Moderation Documentation
Definitely put the codein an own file and the file in a "helper" app (every project has one).