I want to change the url prefix in apache superset. As of now in the browser it looks like '<my_site>/superset/welcome or <my_site>/superset/sqllab . can we change this to something like <my_site>/my_name/welcome etc.
Also how can we add a new menu item in top nav bar ?
For your specific requirement, you can do that by overriding the route_base variable in the Superset class inside /views/core.py
It would look something like:
class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods
"""The base views for Superset!"""
route_base = "/my-app" <=========
Related
I have a django project where url is like this
url(r'^invoice/(?P<invoice_id>[A-Za-z0-9]+)/(?P<order_id>[A-Za-z0-9]+)$',GenerateInvoicePdf,name='invoice'),
which generates url localhost:8000/invoice/2341wq23fewfe1231/3242
but i want url to be like localhost:8000/invoice?invoice_id=2341wq23fewfe1231&order_id=3242
i tried documentation and used syntax like this re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments), But did not get desired result.
how can i do so?
The parts which you are trying to write after ? is called url query string. You don't need to define them in the urls.py. You can just use:
re_path(r'^comments/$', comments),
And inside comments views, you can access the query string like this:
def comments(request):
invoice_id = request.GET.get('invoice_id')
order_id = request.GET.get('order_id')
# rest of the code
Basically i am trying to use .get_absolute_url() to return dynamic links in relative to the current app running, in other words reverse the model url to a different url based on which app being called.
Let me try to explain what i am trying to do by example, We have three apps
Blog (serves as a shared data layer, contains models for post, authors ... etc)
BloggerWay (serves as a view layer number one, uses the Blog app models to display content in some given layout)
TumblrWay (serves as another view layer, again uses the Blog app models to display content in some given layout)
My urls.py files goes like
----------
*Project.urls.py*
----------
urlpatterns= [
url('/blogger/', include(BloggerWay.urls.py, namespace='blogger'),
url('/tumblr/', include(TumblrWay.urls.py, namespace='tumblr'),]
----------
*BloggerWay.urls.py*
----------
urlpatterns= [
url('/post/(?P<id>\d+)', Blog.as_view(), name='blog'),]
----------
*TumblrWay.urls.py*
----------
urlpatterns= [
url('/post/(?P<id>\d+)', Blog.as_view(), name='blog'),]
My question is How can i define .get_absolute_url() method for the Post model so it knows which app we are in and return the correct url to it.
example:
if in BloggerWay return '/blogger/post'
if in TumblrWay return '/tumblr/post'
I know that i can use the reverse() function to get the url for a given named pattern and that it accepts a current_app= argument but my problem is how can i get the running app so i can pass it.
class Post(...):
def get_absolute_url(self):
WhoAmI = ... #get current app here, BUT HOW!
return reverse('post', current_app=WhoAmI)
Solutions that i want to avoid:
I can inherit the Post class in both of the apps, override the .get_absolute_url() there, hardcoding the name space of each app in it. Then use the class in my app instead of directly using the one defined as model/table.(while offcourse avoid performing migrations for that class, even better define it somewhere else than models.py)
Implement another property for get_url() for your class.
The reverse function should includes "namespace:name"
e.g
class Post(...):...
def get_blogger_url(self):
return reverse("blogger:post", kwargs={"id":self.id}) # for Blogger
def get_tumblr_url(self):
return reverse("tumblr:post", kwargs={"id":self.id}) # for Tumblr
I am working on a tiny movie manager by using the out-of-the-box admin module in Django.
I add a "Play" link on the movie admin page to play the movie, by passing the id of this movie. So the backend is something like this:
import subprocess
def play(request, movie_id):
try:
m = Movie.objects.get(pk=movie_id)
subprocess.Popen([PLAYER_PATH, m.path + '/' + m.name])
return HttpResponseRedirect("/admin/core/movie")
except Movie.DoesNotExist:
return HttpResponse(u"The movie is not exist!")
As the code above reveals, every time I click the "play" link, the page will be refreshed to /admin/core/movie, which is the movie admin page, I just do not want the backend to do this kind of things, because I may use the "Search" functions provided by the admin module, so the URL before clicking on "Play" may be something like: "/admin/core/movie/?q=gun", if that response takes effect, then the query criteria will be removed.
So, my thought is whether I can forbid the HttpResponse, in order to let me stay on the current page.
Any suggestions on this issue ?
Thanks in advance.
I used the custom action in admin to implement this function.
So finally I felt that actions are something like procedures, which have no return values, and requests are something like methods(views) with return values...
Thanks !
I've got my mind set on dynamically creating URLs in Django, based on names stored in database objects. All of these pages should be handled by the same view, but I would like the database object to be passed to the view as a parameter when it is called. Is that possible?
Here is the code I currently have:
places = models.Place.objects.all()
for place in places:
name = place.name.lower()
urlpatterns += patterns('',
url(r'^'+name +'/$', 'misc.views.home', name='places.'+name)
)
Is it possible to pass extra information to the view, without adding more parameters to the URL? Since the URLs are for the root directory, and I still need 404 pages to show on other values, I can't just use a string parameter. Is the solution to give up on trying to add the URLs to root, or is there another solution?
I suppose I could do a lookup on the name itself, since all URLs have to be unique anyway. Is that the only other option?
I think you can pass a dictionary to the view with additional attributes, like this:
url(r'^'+name +'/$', 'misc.views.home', {'place' : place}, name='places.'+name)
And you can change the view to expect this parameter.
That's generally a bad idea since it will query the database for every request, not only requests relevant to that model. A better idea is to come up with the general url composition and use the same view for all of them. You can then retrieve the relevant place inside the view, which will only hit the database when you reach that specific view.
For example:
urlpatterns += patterns('',
url(r'^places/(?P<name>\w+)/$', 'misc.views.home', name='places.view_place')
)
# views.py
def home(request, name):
place = models.Place.objects.get(name__iexact=name)
# Do more stuff here
I realize this is not what you truly asked for, but should provide you with much less headaches.
I want to use django's admin filter on the list page.
The models I have are something like this:
class Location(model):
name = CharField()
class Inquiry(Model):
name = CharFiled()
location = ManyToManyField(Location)
Now I want to filter Inquiries, to display only those that contain relation to specific Location object. If I use
class InqAdmin(ModelAdmin):
list_filter = ['location', ]
admin.site.register(Inquiry, InqAdmin)
the admin page displays me the list of all Locations and allows to filter.
What I would like to get, is to get list of only those locations that have some Inquiries in relation to them (so I don't ever get the empty list result after filtering).
How can this be done?
You could create a custom manager for Locations that only returns Locations that have an Inquiry associated with them. If you make this the default manager, the admin will use it.
Only caveat is that you'll need create another manager that returns all Locations and use that in the rest of your app whenever you want to retrieve Locations that don't have an associated Inquiry.
The managers section in the Django docs is quite good, and should be all you need to get this set up.
EDIT:
sienf brings up a good point. Another way to accomplish this would be to define a subclass of django.contrib.admin.SimpleListFilter, and write the queryset method to filter out Inquiries with empty Locations. See https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter