Django: adding simple button to admin generic view - python

Django newbie here working on a pre-existing app I've inherited. I need to add a cancel/back button to the show (DetailView in Django terms, I think?) page. I know how to do this with anchor tags in HTML. However, I can't figure out where the HTML template is for this view! I believe it's being procedurally generated as a "generic view" or "admin" from this code in urls.py:
from django.conf.urls import url
from . import views
app_name = 'reports'
urlpatterns = [
# ex: /reports/
url(r'^$', views.IndexView.as_view(), name='index'),
# ex: /reports/5/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# ex: /reports/5/results/
url(r'^(?P<pk>[0-9]+)/results$', views.ResultsView.as_view(), name='results'),
# ex: /reports/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote$', views.vote, name='vote'),
]
The bottom bar of the reports show / DetailView page contains the following values when accessed via browser:
I'd like to add a "Cancel" button to this bar, but I can't find where I could add such a button -- I can't even find these existing buttons in the codebase.
An admin/reports_admin.py exists with some configuration that may be relevant, but I don't see an obvious place in that file where I could add this "Cancel" button.
Please help!

I think this is what you want
Overriding admin templates | Django docs
as well you can override admin views, which is also described here, to add additinal functionality.

Related

In django how to generate dynamic url after domain name for every page?

I am building a blog website where I set a unique title for every article. I want the article should have url domain_name/<article_title>/.
Suppose I have model A and Moel B:
class A(models.Model):
title = models.CharField(max_length=500,unique=True)
class B(models.Model):
title = models.CharField(max_length=500,unique=True)
app.urls.py file :
urlpatterns = [
path('',view.index,name="index"),
path('contact/', contact, name="contact"),
path('about/', about, name="about"),
path('terms-and-conditions/', terms, name="terms_and_conditions"),
path('privacy/', privacy, name="privacy"),
path('<str:title>/', article_details, name="article_details"),
]
I have view file as follows:
def article_details(request,title):
if 'title_in_model_A':
render 'some_page_A'
if 'title_in_model_B:
render 'some_page_B'
render(request,'app/404.html')
project.urls file:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
handler404 = 'app.views.view_404'
My question is:
Is this type of page rendering good or not?
Does 404 request handles correctly?
The way OP is doing is ok, but if one wills it's possible to simplify the article_details view by using the shortcut get_object_or_404, like
from django.shortcuts import get_object_or_404
def article_details(request,title):
article = get_object_or_404(A, title=title)
In order to customize the 404 view one can use handlers. Here's a good example in the docs.
Hard to say if OP's renders correctly because the question doesn't show that OP has a clothes app with a view_404 in views.py.
As per OP's new requirement, in the case of having two models and wanting to check the existence of instances that have the title matching a specific one, then OP can use exists() as follows
def article_details(request,title):
if A.objects.filter(title=title).exists():
# render page A
elif B.objects.filter(title=title).exists():
# render page A
else:
# 404
Note that this method is good if one doesn't need the model but are just checking the existence. If one does need it as well, then one can include inside of the condition the following (before the render)
my_model = MyModel.objects.get(title=title)

Django (mezzanine) urls catching everything

I'm writing some custom views in the admin of a django project, should be simple. I have an "events" page and i want to create a "event" page (exactly the same as the django polls tutorial but in the admin, the event page would be the same as the detail view.)
No i cannot use the built in functionality as normal using foreignkeys etc and need to build from scratch.
urls.py:
admin.autodiscover()
def get_admin_urls(urls):
def get_urls():
my_urls = [
url(r'^my_cms/events', views.events, name="events"),
url(r'^my_cms/events/(?P<event_id>[0-9]+)/$', views.detail, name='detail'),
]
return my_urls + urls
return get_urls
admin_urls = get_admin_urls(admin.site.get_urls())
admin.site.get_urls = admin_urls
urlpatterns = i18n_patterns("",
("^admin/", include(admin.site.urls)),
)
So..
visiting .../admin/my_cms/events/ works
but .../admin/my_cms/events/xxxxxx just displays the same events page, rather than the detail view
if i change the url pattern to anything other than "events" eg:
url(r'^my_cms/events', views.events, name="events"),
url(r'^my_cms/[anything]/(?P<event_id>[0-9]+)/$',
then it will display the event view correctly...
So my question is why is the first url catching everything? i can put anything (.../admin/my_cms/events/anythingilike) and it will display the events page?
Joe
Because r'^my_cms/events' doesn't have a $ at the end. That means, only the beginning of this URL is checked, not the ending.
To illustrate, r'^my_cms/events' will match any URL that begins with 'my_cms/events', example:
'my_cms/events'
'my_cms/events/xxxxxx'
'my_cms/eventsxxxxxx'
So, even if you make a request to the detail view at 'my_cms/events/xxxxxx', the events page URL is matched. Once Django finds a match, it doesn't check further URLs and calls the related view.
To fix, add a $ sign at end of your URL regexp, like this:
url(r'^my_cms/events/$', views.events, name="events"),

How to djangocms-link to a app-hook subpage?

I have an app that provides a form to create and update "Prediction"s.
urls.py:
urlpatterns = patterns('',
url(r'^create/$', PredictionCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)/$', PredictionDetailView.as_view(), name='detail'),
url(r'^$', PredictionListView.as_view(), name='list'),
)
The app is attached with an app-hook to Django CMS 3.0.12.
cms_app.py:
class Predictionhook(CMSApp):
name = "Predictionhook"
urls = ["prediction.urls"]
app_name = "prediction"
apphook_pool.register(Predictionhook)
Is there an easy way to include the 'create' URL of the attached app in the list of possible choices of pages in the djangocms-link plugin?
The standard setup won't do it as far as I'm aware.
I'd create your own copy of djangocms-link and extend it to give a field to take the same thing you'd put in a template URL tag. Then just set up the template to render that field value as a standard Django URL template tag.
So in your new field of the link app you'd add prediction:create and then in the template being rendered you'd have {% url link.myurllinkfield %}

Modifying and creating sites in djangos admin app

I have to include multiple changes to djangos admin panel, so I decided to fork the django admin app into my own django project.
As I was working with this admin app I recognized, that the site registration and template handling differs from the apps, that are normally created in django.
For instance, I want to keep the old admin index.html template and view, for backup and safety reasons but the landing page should be replaced by a custom page.
For that of course I need to change admin/templates/index.html and /admin/sites.py respectively.
I copied the old index function in admin/sites.py to old_index.py and created a old_index.html in the template folder.
But if I try to reference to old_index.html in my new index.html with
old index
I got an NoReverseMatch-Exception thrown. Unfortunately I did not found more information about how the django admin app itself register new views and sites, so an example or description would be helpful.
Creating separate views for the admin app in the distinct other apps in my project is no real option, due the high amount of changes, that need to be done.
The main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'django_project.views.home', name='home'),
url(r'^polls/', include('other_app.urls', namespace="other_app")),
url(r'^admin/', include(admin.site.urls)),
)
The admin app itself does not provide a urls.py file and the views.py is exactely the same as in django.contrib.admin I just copied the function index to a new function called old_index, referencing to a template old_index.html.
Maybe the point did not get so clear, as I expected. I copied the whole admin app in my project and want to add a custom defined site to it, regardless where. But I failed to understand how sites and views are registered in the admin app itself, because the way is different from the custom apps you create normally in django.
So, is it possible (and how) to add a custom site in the django.contrib.admin app?
I think you need to create your own AdminSite for custom purposes and keep default as it is. More about this you can find here: https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#adminsite-objects and here https://docs.djangoproject.com/en/dev/ref/contrib/admin/#multiple-admin-sites-in-the-same-urlconf
Update:
You need to edit get_urls method of AdminSite class - add:
url(r'^$', wrap(self.old_index), name='old_index')
to urlpatterns variable. And rename old index method to old_index.

Modify Django admin app index

I want to change the app index page so I add help text to the models themselves, e.g. under each model I want to add help text. I know that I should override AdminSite.app_index. What is the best way to do this?
I can create a new AdminSite subclass, and override app_index method to send the help text to the template. In urls.py I can use an instance of MyAdminSite instead of django's vanilla AdminSite.
# urls.py
from mysite.admin import MyAdminSite
site = MyAdminSite()
urlpatterns = patterns('',
(r'^admin/', include(site.urls)),
)
# app/admin.py
site.register(MyModel)

Categories

Resources