I'm using the Django 1.3 alpha to create a project with two applications. I use 1.3 because of the class-based views. For these two applications I have a set of common base view-classes, that are inherited by the actual views in the applications. In the base class, is there a way to find out what application the view is "called" from? E.g. can I use the URL to get the "current" application?
If you are inheriting from the generic list and detail views that django provides you could access self.model to gain access to the model that the view displays information about, otherwise you will probably have use django's resolve(): resolve(self.request.path).
You also could make your own View subclass that you call with a keyword of your choice:
# views.py
from django.views.generic.base import View
class MyView(View):
app_name = None
# urls.py
from django.conf.urls.defaults import *
from some_app.views import MyView
urlpatterns = patterns('',
(r'^myview/', MyView.as_view(app_name='app_name')),
)
Then you should be able to access it through self.app_name.
Related
I am not able to register an APIView to my url routes.
Code from views :
class PayOrderViewSet(APIView):
queryset = PayOrder.objects.all()
Code from urls :
router = routers.DefaultRouter()
router.register(r'document/payorder', PayOrderViewSet)
This newly created url doesn't exist at all.
What is solution for this?
Routers and APIViews (generic or otherwise) are two different ways to create API endpoints. Routers work with viewsets only.
In your code, you are although trying to create a viewset for a router your code is extending APIView class.
Your problem will be taken care by what #linovia has suggested in his asnwer. I would suggest it will be good idea to understand the difference between those two.
GenericViewSet inherits from GenericAPIView but does not provide any implementations of basic actions. Just only get_object, get_queryset.
ModelViewSet inherits from GenericAPIView and includes implementations for various actions. In other words you dont need implement basic actions as list, retrieve, create, update or destroy. Of course you can override them and implement your own list or your own create methods.
Read More about viewsets and Generic Class Based APIViews :
Routers won't work with APIView. They only work with ViewSets and their derivatives.
You likely want:
class PayOrderViewSet(ModelViewSet):
For ViewSet you use router for url register and for APIView you need to add path to urlpatterns. Next example should help:
from post.api.views import UniquePostViewSet
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from post.api.views import FileUploadView
router = DefaultRouter()
router.register('UniquePost', UniquePostViewSet, base_name='uniquepostitem')
urlpatterns = [
path('demo', FileUploadView.as_view(), name='demo'),
]
urlpatterns += router.urls
I implemented authentication management using Django auth with the default admin site but then I wanted to use my own AdminSite to rewrite some behaviors:
class OptiAdmin(admin.AdminSite):
site_title = "Optimizer site's admin"
#...Other stuff here
Then registered my own models:
admin_site = OptiAdmin(name='opti_admin')
admin.site.register(MyModel, MyModelAdmin)
#Other stuff here
But when I go to the admin site I am only able to see the models I just registered, which sounds fair to me but I would like to see all the other apps models in this new custom site including the auth's users and groups and I don't know how to do this automatically like the default admin does, pls help :).
Create your own AdminSite with a simple __init__() override.
Import your admin in urls.py.
Replacing the Django Admin and getting the autodiscover() behavior is possible with minimal effort. Here's a project structure generated in the typical django-admin startproject project fashion:
project/
manage.py
project/
__init__.py
settings.py
urls.py
wsgi.py
admin.py # CREATE THIS FILE
project/admin.py: (I think it makes the most sense to do this at the project level.)
from django.contrib.admin import * # PART 1
class MyAdminSite(AdminSite):
site_header = "My Site"
def __init__(self, *args, **kwargs):
super(MyAdminSite, self).__init__(*args, **kwargs)
self._registry.update(site._registry) # PART 2
site = MyAdminSite()
project/urls.py (snippet):
from . import admin # PART 3
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
Part 1 is simple Python. By importing everything from django.contrib.admin into your namespace, it acts as a drop-in replacement. I suppose you don't have to do this, but it helps preserve expectations. Part 3, simply connect up your admin. Part 2 is the real trick. As the documentation says, autodiscover() is called to do the work. All autodiscover does is go through INSTALLED_APPS attempting to import a file called admin.py. Importing runs the code of course and that code is doing the same thing you do to register models (example by decorator and example by method). No magic. You don't have to register your models with your customized admin (as the documentation says).
Autodiscover looks smarter than it is with its register_to kwarg. That indicates you could call autodiscover() yourself passing your own admin. Nope; there's no wiring connected there (future feature?). The assignment happens here and is fixed to the native AdminSite instance here (or here using the decorator). Django contrib models register to that instance and so will any third-party libraries. It's not something you can hook into.
Here's the trick though, _registry is just a dictionary mapping. Let Django autodiscover all the things and then just copy the mapping. That's why self._registry.update(site._registry) works. "self" is your customized AdminSite instance, "site" is Django's instance and you can register your models with either.
(Final note: If models are missing, it's because of import order. All the registration to Django's AdminSite needs to happen before you copy _registry. Registering directly to your customized admin is probably the easiest thing.)
The Django docs suggest using SimpleAdminConfig with a custom admin site.
INSTALLED_APPS = (
...
'django.contrib.admin.apps.SimpleAdminConfig',
...
)
That prevents the models being registered with the default AdminSite.
The docs seem to assume that you will import the models individually and add them to your custom admin site:
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
admin_site.register(Group, GroupAdmin)
admin_site.register(User, UserAdmin)
This would be very repetitive if you have models in many apps. It doesn't offer any advice how to automatically register models from all your apps with your custom site.
You could try monkey patching admin, and replacing admin.site with your own.
from django.contrib import admin
admin.site = OptiAdmin(name='opti_admin')
Then, when code called admin.site.register(), it would register the model with your admin site. This code would have to run before any models were registered. You could try putting it in the AppConfig for your app, and make sure that your app is above django.contrib.admin.
Adding to JCotton's great answer:
Using django 2.0, overriding site_header and site_title in the custom admin site only works for the index page.
To get it to work with all admin views, extend JCotton's code with the following:
def __init__(self, *args, **kwargs):
super(MyAdminSite, self).__init__(*args, **kwargs)
self._registry.update(site._registry) # PART 2
for model, model_admin in self._registry.items():
model_admin.admin_site = self
Just include init method in your CustomAdminSite class like this.
class CustomAdminSite(admin.AdminSite):
def __init__(self, *args, **kwargs):
super(CustomAdminSite, self).__init__(*args, **kwargs)
self._registry.update(admin.site._registry)
I've got a django app that has it's own urlconf included into the main one. Every page in this app is protected by a separate set of perms not granted to normal users. Think employees work view as opposed to users' profiles etc.
I'm using classed based views, so right now I've got the landing view's dispatch() checking perms, but with this method I'm going to have to do that for every view. That just isn't DRY.
So my options as I see them are:
Create a mixin that specifically checks for this permission
manually check using dispatch() in each view
Somehow check at the url level
Is there any way to set a permission requirement on the entire url inclusion? They all currently have login_required() on.
Easy!
from django.contrib.auth.decorators import login_required
urlpatterns = patterns('',
url(r'^foo/', login_required(include('foo.urls'))),
)
Update
You want to check user permissions, not user authentication. Easy too:
from django.contrib.auth.decorators import user_passes_test
urlpatterns = patterns('',
url(r'^foo/', user_passes_test(lambda u: u.has_perm('perm'))(include('foo.urls'))),
)
I am new to Django and would greatly appreciate your help.
I have the below mentioned piece of code from an older book about Django. However, django.views.generic.list_detail has been deprecated. Can someone tell me how I could re-write this code with django.views.generic.list.ListView?
from django.conf.urls import patterns, include, url
from cmsproject.cms.models import Story
info_dict = {'queryset': Story.objects.all(), 'template_object_name': 'story'}
urlpatterns = patterns('django.views.generic.list_detail',
url(r'^(?P<slug>[-\w]+)/$', 'object_detail', info_dict, name="cms-story"),
url(r'^$', 'object_list', info_dict, name="cms-home"),
)
Assuming all you want to do is fetch a list of Story model objects, this is one way to write your views.py and urls.py:
In views.py:
from django.views.generic.list import ListView, DetailView
from cmsproject.cms.models import Story
class StoryListView(ListView):
model = Story
template_name = "cms/story_list.html"
class StoryDetailView(DetailView):
model = Story
template_name = "cms/story_detail.html"
template_name depends on where in the project you placed your html files. By setting model = Story, ListView will fetch Story.objects.all(). To customize, filter, add context etc., you can override any of the methods that your class based view inherits from its parent view (ex. in StoryListView you can override ListView methods).
In urls.py
from django.conf.urls import patterns, url
from cmsproject.cms.views import StoryDetailView, StoryListView
urlpatterns = patterns('',
url(r'^(?P<slug>[-\w]+)/$', StoryDetailView.as_view(), name="cms-story"),
url(r'^$', StoryListView.as_view(), name="cms-home"),
)
Think of urls.py as a mapping between the url and the View object. Defining name allows you to refer/link to other views by including name as a parameter to url template tag in the templates.
Some Extremely Useful References:
Effective Django - Class Based Views
CCBV List View
Django Project Generic Display Views
Built-in template Tags and filters -> look under url
Well, let me explain this.
I am working on a simple django admin project.
In the admin.py file, I have the following admin classes:
class A_Admin(admin.ModelAdmin):
#some stuff
class B_Admin(admin.ModelAdmin):
#some stuff
I want to override the get_urls() method of A_Admin that if I click a button on A_Admin instance change page, it will redirect the page to B_Admin changelist page.
(I know there are many ways to do what I want and what I mentioned above is not the best, but this is what I want. So let skip the discussion why I insist on this solution.)
I want to the following:
def get_urls(self):
#django's code
#inside the urlpattern
urlpattern = (
#default urls from django admin
.....
url(r'^some_url$',
wrap(super(B_Admin, self).changelist_view),
name='%s_%s_delete' % info),
....)
return urlpatterns
This is not working, since 'self' is a A_Admin class object rather than B_Admin obejct.
So is there any way to get the proxy of calss A_Admin inside B_Admin?
I just wanna override changelist_view of A and call it inside B.
Is this possible?
Thanks in advance
You should just instantiate B_Admin and use its method.
I believe the following code should work:
from django.contrib import admin
from my_app.models import B_Model # The model for which B_Admin is used
def get_urls(self):
#django's code
#inside the urlpattern
urlpattern = (
#default urls from django admin
.....
url(r'^some_url$',
wrap(B_Admin(B_Model, admin.site).changelist_view),
name='%s_%s_delete' % info),
....)
return urlpatterns
UPDATE: Most probably, B_Admin was already instantiated when you called
admin.site.register(B_Model, B_Admin)
So instead of doing
B_Admin(B_Model, admin.site)
again you can just get it from the AdminSite's registry:
admin.site._registry[B_Model]