No reverse match error. How to debug? - django

I am trying to link the databrowse.admin widget of django that rests here :
http://127.0.0.1:8000/admin/openmaps/open_layers/
I tried to put this in a template and it returned a reverse match error. How to debug ?
A

The URL Tag you are attempting to use is specified in the Django Documentation here (for version 1.4):
https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url
It's purpose is to keep the URLs in your links DRY (Don't Repeat Yourself), so that you don't have to change your link URLs between your dev, staging, production or any other server environments you may have.
The url tag takes a view or a reference to a view via a url name as its main argument, and any arguments that the view takes as second arguments. From the documentation:
{% url path.to.some_view v1 v2 %}
Where path is a package name, to is a module name and some_view is a view function. v1 and v2 are args that the view takes. It would look like so in path/to.py:
from django.http import HttpResponse
def some_view(request, v1, v2):
return HttpResponse("A response")
Furthermore, when dealing with the admin, you'll need to use the namespace admin using the URL namespace strategy, like so:
{% url admin:view_name %}
What you need to do is find the path to the view you are looking for, and create the URL using that path. To get you started, you can create a link to you your admin site index like so:
My Admin Site
These will create links for the admin logout, password change form, and app list, respectively:
Admin Logout
Change Password
The Application List
For the views on specific models within the admin, django uses the meta data on the models to construct their url names. You can do the same with your models to link to their admin pages, however, you'll need to construct their names programmatically (unless you know them). So if you had a model named Foo, you could link to its changelist view, add view, and delete view in the admin respectively by constructing their view names and using the reverse method on them:
In your view:
from django.core.urlresolvers import reverse
#...some view code...
#Get an instance of the model
bar = Foo.objects.all()[0]
prefix = "%s_%s_" % (Foo._meta.app_label, Foo._meta.module_name)
changelist_name = "%schangelist" % prefix
add_name = "%sadd" % prefix
delete_name = "%sdelete" % prefix
changelist_url = reverse(changelist_name)
add_url = reverse(add_name)
delete_url = reverse(delete_name, args=(bar.pk,)) #You need the id of the model you want to delete as an argument.
#...some more view code...
In your template
The Foo ChangeList
Add a Foo
Delete {{ bar.name }}
You'll likely have to do some digging into the guts of django or any particular extension you're using to get the exact url name that you want. If you can provide more detail about the model you're trying to access in the admin, I can provide a more specific answer.

Related

Django Allauth very specific redirection after Facebook Social Signup

I know there are a few questions on the topic already but I have tried to implement those solutions and could not really solve my problem.
I am talking about social signup with allauth here, and facebook in particular.
DESIRED BEHAVIOR: after facebook signup I want user to go to my url "accounts:welcome", but when they simply login I want them to go to my LOGIN_REDIRECT_URL (which is the site's home page).
After looking here and there this is the code I came up with (writing my custom adapter)
settings.py:
LOGIN_REDIRECT_URL = ("gamestream:home")
SOCIALACCOUNT_ADAPTER = "myproject.users.adapter.MySocialAccountAdapter"
adapter.py:
from django.conf import settings
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
class MySocialAccountAdapter(DefaultSocialAccountAdapter):
def save_user(self, request, sociallogin, form=None):
print('OK11OK')
super().save_user(request, sociallogin, form=form)
return redirect(reverse('accounts:welcome'))
def get_connect_redirect_url(self, request, socialaccount):
print('OK22OK')
assert is_authenticated(request.user)
url = reverse('accounts:welcome')
return url
Please assume that all links/settings are good as for example the console prints out 'OK11OK' when I create myself as a user via the facebook app. The fact is that the method get_connect_redirect_url never gets triggered as I never read 'OK22OK' on the console.
The user is created and I end up on the home page, which is not what I want.
So I thought that after the save_user method something else gets called as I can tell that I pass through accounts:welcome, but then end up on the home page.
I can tell this because if I return an incorrect url in the save_user method I get an error that is specific to that url on that line.
So, what is wrong here?
I think I might be overriding the wrong method but I have read all the code of the base SocialAccountAdapter and I can't see anything else that would be the right choice.
Just wanted to mention that as I have more control on the normal account signup (not social) I have achieved what I wanted.
Any ideas?
Thanks very much!
I had the same problem too, I found two methods:
METHOD 1
Django doesn't use redirection function of the DefaultSocialAccountAdapter, you'll have to override the get_signup_redirect_url function of DefaultAccountAdapter to achieve the result.
First you need to change the default adapter in settings.py:
ACCOUNT_ADAPTER = 'users.adapter.MyAccountAdapter'
Then, just override the get_signup_redirect_url with your url:
# project/users/adapter.py
from allauth.account.adapter import DefaultAccountAdapter
class MyAccountAdapter(DefaultAccountAdapter):
def get_signup_redirect_url(self, request):
return resolve_url('/your/url/')
METHOD 2
This is the easier one
If you take a look at the source code at DefaultAccountAdapter it says:
Returns the default URL to redirect to after logging in. Note
that URLs passed explicitly (e.g. by passing along a next
GET parameter) take precedence over the value returned here.
So, you can pass along a next parameter in your login template to force the redirection. Here is an example using Google social login:
{% load socialaccount %}
{% providers_media_js %}
{# your html tags #}
<body>
SOCIAL LOGIN
</body>
Of course you can personalize the next url (I'm refering to /success/url/) as you wish. You can pass a context variable with your desired url and put it there instead of that hardcoded url.

How to add content to the index page using Flask-Admin

I am using flask-admin, and I want to add a dashboard to the home page. I found I can add a new page using:
admin = Admin(name='Dashboard', base_template='admin/my_master.html', template_mode='bootstrap3')
then:
admin.init_app(app)
and finally I added my_master.html, and added content. However, it is all static, how can I add custom data to that view?
I found the answer in the documentation: http://flask-admin.readthedocs.org/en/latest/api/mod_base/
It can be overridden by passing your own view class to the Admin constructor:
class MyHomeView(AdminIndexView):
#expose('/')
def index(self):
arg1 = 'Hello'
return self.render('admin/myhome.html', arg1=arg1)
admin = Admin(index_view=MyHomeView())
Also, you can change the root url from /admin to / with the following:
admin = Admin(
app,
index_view=AdminIndexView(
name='Home',
template='admin/myhome.html',
url='/'
)
)
Default values for the index page are:
If a name is not provided, ‘Home’ will be used.
If an endpoint is not provided, will default to admin Default URL route is /admin.
Automatically associates with static folder. Default template is admin/index.html
According to flask-admin documentation use this:
from flask_admin import BaseView, expose
class AnalyticsView(BaseView):
#expose('/')
def index(self):
return self.render('analytics_index.html', args=args)
admin.add_view(AnalyticsView(name='Analytics', endpoint='analytics'))

Dynamically alter urls.py to support dynamically created URLs?

I'd like to create a web service using Django that dynamically adds URLs to my urls.py file. Can this be done in Django? In other words, I'd like to have users be able to sign up for an end point that gets dynamically created using Django, e.g. my domain "dynamicurl.com" could add /johnp/ via a registration of user johnp. How would I do this?
Just create a pattern that matches the required characters for your username. Here is an example:
url(r'(?P<username>[\w.#+-]+)/$',
'yourapp.views.user_home', name='user-home'),
Then, when someone goes to yourdomain.com/johnp/ in your view you can do something like:
def user_home(request, username=None):
return render(request, 'user_home.html', {'username': username})
In user_home.html:
<strong>Welcome {{ username }}</strong>
Which will result in:
Welcome johnp
Consider this situation.
Suppose at some point in time you have a million users, your urls.py file will have a million records for user pages only. And I hope you do not wish to have separate views to handle all these separate urls.
Therefore, it is better define url patterns that can dynamically alter the content inside the templates depending on the value received within the url.
Using class based views, this can be done as follows:
In your urls.py file, write
url(r'^(?P<user_name>(.*))/$',ProfileView.as_view(),name='profile-view'),
class ProfileView(TemplateView):
template_name = "abc.html"
def get_context_data(self,**kwargs):
context = super(ProfileView,self).get_context_data(**kwargs)
context['user_name'] = kwargs['user_name']
return context
Then, in your template you can use it as {{user_name}}.

Pass a query parameter with django reverse?

I have a url that is meant to be accessed like
/people/raj/updates
/people/raj/updates?tag=food
But Django reverse URL resolver seems to have no provision to do tag=food, that is to detect it as an extra parameter and put in the query string.
How do I pass query parameters?
It depends on whether you are building the URL in the python code or in a template.
In python code (e.g. the view):
from django.http import QueryDict
query_dictionary = QueryDict('', mutable=True)
query_dictionary.update(
{
'tag': 'food'
}
)
url = '{base_url}?{querystring}'.format(
base_url=reverse(my.url.name),
querystring=query_dictionary.urlencode()
)
And in a template:
My Link
You caould also pass the QueryDict object from the view to the template and use that when building the URL in the template:
My Link
Django's reverse does not include GET or POST parameters. They are not part of the url.
You can of course always create the url by, for instance in a template, attaching the parameter as in:
{% url 'named_url' %}?tag=food
This way it gets attached anyway. Alternative is building an url regex that includes the possible tag, like:
url(r'^/people/raj/updates/(?P<tag>[a-zA-Z0-9]+/)?', yourview())
This way you can check for the kwarg tag in your view.

For a django model, how can I get the django admin URL to add another, or list objects, etc.?

As much as I love the django documentation, the section on bookmarklets in the admin is strangely vague.
My question is this: If I'm in a view and I have a django model (or, in some cases, an actual object), how can I get to the relevant admin pages for that model (or object)? If I have the object coconut_transportation.swallow.objects.all()[34], how can I jump right to the admin page to edit that particular swallow?
Likewise, how can I get the URL for the admin page to add another swallow?
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls
obj = coconut_transportation.swallow.objects.all()[34]
# list url
url = reverse("admin:coconut_transportation_swallow_changelist")
# change url
url = reverse("admin:coconut_transportation_swallow_change", args=[obj.id])
# add url
url = reverse("admin:coconut_transportation_swallow_add")
You can retrieve this from the actual object instance, this worked for me:
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(object.__class__)
object_admin_url = django.core.urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(object.pk,))
See this: http://djangosnippets.org/snippets/1916/
You can actually retrieve the info without making a query to ContentTypes
Just add this to your model class:
def get_admin_url(self):
return urlresolvers.reverse("admin:%s_%s_change" %
(self._meta.app_label, self._meta.model_name), args=(self.pk,))