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?
Related
I'm new to Django and I'm facing a problem that I can't solve despit my research... I'v already made small django projects without this error but since I reformat my computer (I can't see the link but that's context!) i'm having this problem.
So when I'm running a django project this is what my browser shows me despite others urls in my project
Django doesn't go trhough my urls
I'v tried with an almost empty project but a least one more url and still have the same page.
urls.py in the main file (src/mynotes/urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('index/', include('dbnotes.urls')),
path('admin/', admin.site.urls),
]
settings.py in the main project (src/mynotes/settings.py):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dbnotes'
]
urls.py in the app (src/dbnotes/urls.py):
from django.urls import path
from . import views
urlpatterns = [
path("", views.homepage, name="homepage"),
]
views.py in the app (src/dbnotes/views.py):
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello, world. You're at the polls index.")
Even stranger, when i change "path('admin/', admin.site.urls)" I still get the admin page with the same route. Even if i change 'admin/' or admin.site.urls.
It looks like my local server display an over empty django project but in the error page it's written "mynotes.urls" as the url location which is the good folder.
I'm working with a virtual environment.
I'm lost as it's the first time i got this issue despite having created ohter (small) django projects!
If anyone has an idea that would be great! Thank you!
The directory:
Directory
I tried to follow the django documentation to write the smaller project and then having less room to error!
I tried with a new project and a new virtual envirnoment.
I found a topic related to my problem but without an anwer : Django doesn't see urls.py
What is the simplest way to add a view to my Django (3.2) admin UI? That is, add a URL mysite.com/admin/my-view so that visiting that URL acts like the rest of admin (in particular, requires similar permissions).
There is a whole page on this, but it's not obvious to me how to piece it together.
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#adding-views-to-admin-sites says you can add a get_urls to your AdminSite class. Okay, so I need my own AdminSite class. And I need to register it in apps, maybe?
I did this:
class MyAdminSite(admin.AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path('my-view', self.my_view, name='my-view')
]
return my_urls + urls
def my_view(self, request):
# do something ..
admin_site = MyAdminSite(name='my_admin')
and this in urls.py:
from .admin import admin_site
urlpatterns = [
path('admin/', admin_site.urls),
and this in unchanged in INSTALLED_APPS in settings:
'django.contrib.admin',
But, now it only shows the admin for the one app, instead of for all the apps. So how do I get it to auto-discover all the apps like it used to? Or is there a simpler way?
P.S. There is also a question on this, but the answer didn't have enough detail for me to use it.
EDIT: I'm reading Make new custom view at django admin and Django (1.10) override AdminSite ..
EDIT 2: This was a hack, but it works for me:
from django.contrib.admin import site
admin_site._registry.update(site._registry)
I had to update in the right place (the project urls.py), perhaps when all the other admin stuff is already loaded.
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.
My issue is my URL is coming up mysite.com/test-news-1/ instead of mysite.com/news/test-news-1/
I've started a new project using cookiecutter-django. I then wanted to add wagtail cms to the project and followed the docs to do so.
I get the wagtail admin page up fine, at /cms/ instead of /admin/ like it says to do on this page - http://docs.wagtail.io/en/v1.3.1/getting_started/integrating_into_django.html
I added a few apps just to practice and get used to wagtail, one directly copied from the wagtail blog example. This is where my issue starts.
The wagtail admin Explorer does not list my apps like it shows on the wagtail site https://wagtail.io/features/explorer/, instead it just says "Welcome to your new Wagtail site!" When I select Add Child Page it allows me to select the app pages I have set up and seems to go by my models just fine. But when I post something and click go to live site it comes up as mysite.com/blog1/ instead of mysite.com/blog/blog1/
I believe my problem that I dont understand the final part of the doc page that I linked above. It says,
Note that there’s one small difference when not using the Wagtail
project template: Wagtail creates an initial homepage of the basic
type Page, which does not include any content fields beyond the title.
You’ll probably want to replace this with your own HomePage class -
when you do so, ensure that you set up a site record (under Settings /
Sites in the Wagtail admin) to point to the new homepage.
I tried adding the homepage model from the doc page, but this didn't seem to help at all.
I'm very inexperienced, this is my urls.py file, if you need to see other files please let me know.
urls.py
from __future__ import unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name="about"),
# Django Admin, use {% url 'admin:index' %}
url(settings.ADMIN_URL, include(admin.site.urls)),
# User management
url(r'^users/', include("contestchampion.users.urls", namespace="users")),
url(r'^accounts/', include('allauth.urls')),
# Your stuff: custom urls includes go here
# Wagtail cms
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception("Bad Request!")}),
url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception("Permission Denied")}),
url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception("Page not Found")}),
url(r'^500/$', default_views.server_error),
]
These two url confs are in conflict =
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'', include(wagtail_urls)),
One must change, otherwise Django will always resolve the base url of `yourdomain.com/' to the first entry. One easy way to fix this is to update the second-
url(r'^content/', include(wagtail_urls)),
Now your Wagtail root page will be accessible at yourdomain.com/content.
As for mysite.com/blog/blog1/ not coming up, that Url would assume that, from your Wagtail root page, there's a page w/ slug 'blog', and then a child page of that with slug blog1. The tree structure of your Wagtail site determines the URLs by default. If you want to override that you'll have to use the RoutablePageMixin as described here.
When you extend AdminSite to create another admin site how do you go about being able to reverse match each site? It seems the admin namespace is hardcoded reverse('admin:index'), is there a way to supply a custom namespace?
You may be confused with the namespace in django. If you are interested to clarify that confusion, you may read up the discussion here.
If you want to try solve your problem, there is a specific documentation for multiple admin sites.
Below are example solutions mostly copied from official documentation
Example Solution:
# urls.py
from django.conf.urls import url
from .sites import basic_site, advanced_site
urlpatterns = [
url(r'^basic-admin/', basic_site.urls),
url(r'^advanced-admin/', advanced_site.urls),
]
And
# sites.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
site_header = 'Monty Python administration'
basic_site = MyAdminSite(name='myadminbasic')
advanced_site = MyAdminSite(name='myadminadvanced')
Reversing
reverse('myadminbasic:index') # /basic-admin/
reverse('myadminadvanced:index') # /advanced-admin/