After making an entry in django admin, django routes directly to entries page. What I want is to go to main admin page. Do I need to overwrite the admin views file or is there a way to extend it and change the url routing?
You can override the response_add() method (or response_change() for editing existing entries) inside your ModelAdmin class as follow:
def response_add(self, request, obj, post_url_continue=None):
return redirect('/admin') # Or whatever URL you want
Related
Is it possible to direct a user back to the previous page coming from the admin dashboard?
I have an "edit" button, and since the "users" who will be able to edit data are savvy enough, I'd rather just have this direct to a change page in admin.
Here is my link:
<td><i class="icon ion-md-create"></i></td>
Use of ?next= from admin doesn't seem to work. For now I'm just having it opened in a different tab, but I'd like to at least know if there is a direct restriction, or if there's more to it when interacting with admin.
Here's an example:
User goes to /stores/1617/
User clicks "Edit"
"Edit" directs them to /admin/inv/lifesafety/1617/change/?next=/stores/1617
After User submits form, User is directed back to /stores/1617
There's no built-in way to do this, but you can accomplish it another way by overriding the response_change method of the LifeSafety admin class.
from django.shortcuts import redirect
class LifeSafetyAdmin(admin.ModelAdmin):
def response_change(self, request, obj):
next = request.GET.get('next')
if next:
return redirect(next)
return super().response_change(request, obj)
I am working on an app where the user will be able to login to his profile. I am using Django-registration-redux. I am using the below code to inculde in my project.
LOGIN_REDIRECT_URL = '/profile/view/(?P<pk>[0-9]+)/'
I want to redirect the user to his profile after logging in. I know that is not the way you can actually call an url in settings file. any solution for the problem?
I believe you can use https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse-lazy to add URL resolution to a setting.
You don't need to pass the user pk to the view. You can get this value and every other data field of the user from request.user object.
As stated in Django docs, you could do something like this:
def profile(request):
if request.user.is_authenticated:
# Do something for logged-in users.
request.user.do_something()
[...]
else:
# Do something for anonymous users like redirect to registration
pass
I'm using userena for handling the users' profiles. I created an app that override some of userena views and urls.
In particular I've created two different signup forms, so now I have two separate urls:
url(r'^signup/customer/$',....
url(r'^signup/owner/$',...
The original userena signup form was accessible at r'^signup/$'.
Question: How do I override the userena original signup url in order to make it unavailable?
The original url should not be accessible to anyone, so I guess Django should show a 404 page.
In your root urls.py conf, just override the url which you want to disable and direct it to Django 404 (page not found) view:
from django.views.defaults import page_not_found
url(r'^signup/$',
page_not_found,
name='userena_signup'),
If you are already overriding some views and URLs, you could override the signup URL with a view that just returns a 404 response.
I'm using django-socialregistration to manage my site's connection with Facebook.
When a user clicks the "Connect with Facebook" button, I am able to automatically create a new Django user and log them in. However, I also need to create a UserProfile (my AUTH_PROFILE_MODULE) record for them which contains their Facebook profile information (email, name, location).
I believe I need to override socialregistration's "setup" view so I can do what I need to do with UserProfile. I've added the following to my project's urls.py file:
url( r'^social/setup/$', 'myapp.views.socialreg.pre_setup', name='socialregistration_setup'),
My custom view is here "/myapp/views/socialreg.py" and looks like:
from socialregistration.forms import UserForm
def pre_setup(request, template='socialregistration/setup.html',
form_class=UserForm, extra_context=dict()):
# will add UserProfile storage here...
return socialregistration.views.setup(request, template, form_class, extra_context)
The socialregistration view signature I'm overriding looks like this:
def setup(request, template='socialregistration/setup.html',
form_class=UserForm, extra_context=dict()):
...
I'm getting the error "ViewDoesNotExist at /social/setup/: Could not import myapp.views.socialreg. Error was: No module named socialregistration.views" when I try the solution above.
The socialregistration app is working fine when I don't try to override the view, so it is likely installed correctly in site-packages. Anyone know what I'm doing wrong?
OK, as Tim noted, this particular problem was path related.
Bigger picture, the way to accomplish what I wanted (creating a linked UserProfile when django-socialregistration creates a user) is best done by passing in a custom form into socialregistration's "setup" view, as the author suggested here: http://github.com/flashingpumpkin/django-socialregistration/issues/issue/36/#comment_482137
Intercept the appropriate url in your urls.py file:
from myapp.forms import UserForm
url('^social/setup/$', 'socialregistration.views.setup',
{ 'form_class': UserForm }, name='socialregistration_setup'),
(r'^social/', include('socialregistration.urls')),
You can base your UserForm off socialregistration's own UserForm, adding in code to populate and save the UserProfile.
I would like to use the django contrib.admin pages to edit my models, but call individual change page from my own views, an then return there after user clicks "save".
Ideally this should happen by appending the return URL to the admin page's url (as in "...?_return_url=)
Any hints?
django.contrib.admin.options.ModelAdmin objects have a response_change method which you can override in a subclass to determine the response which should be returned after an object has been successfully saved - you could override this to return an appropriate HttpResponseRedirect for the object which was just saved.