Django - Static content display based on URL - django

I'm working on a Django site with a basic three column design. Left column navigation, center column content and right column URL specific content blocks.
My question is about the best method of controlling the URL specific content blocks in the right column.
I am thinking of something along the lines of the Flatpages app that will make the content available to the template context if the URL matches a pre-determined pattern (perhaps regex?).
Does anyone know if such an app already exists?
If not, I am looking for some advice about the best way to implement it. Particularly in relation to the matching of patterns to the current URL. Is there any good way to re-use parts of the Django URL dispatcher for this use?

Django CMS is a good suggestion, it depends on how deep you want to go. If this is just the beginning of different sorts of dynamic content you want then you should go that way for sure.
A simple one-off solution would be something like this:
You would just need to write a view and add some variables on the end of the URL that would define what showed up there. Depending on how fancy you need to get, you could just create a simple models, and just map the view to the model key
www.example.com/content/sidecontent/jokes/
so if "jokes" was your block of variable sidecontent (one of many in your sides model instances) the urls.py entry for that would be
(r'^content/sidecontent/(?P<side>)/$,sides.views.showsides),
and then in your sides app you have a view with a
def showsides(request, side):
Sides.objects.get(pk=side)
etc...

For something like this I personally would use Django CMS. It's like flatpages on steroids.
Django CMS has a concept of Pages, Templates, and Plugins. Each page has an associated template. Templates have placeholders where you can insert different plugins. Plugins are like mini-applications that can have dynamic model-based content.

Although Django-CMS is an interesting suggestion, there are quite a few projects that do specifically what you've requested - render blocks of content based on a URL. The main one that I know about is django-flatblocks.

Related

Django how to make every view accept a kwarg?

I have a lot of apps running on my site and I was wanting to make all the views accept a certain kwarg without having to go in and edit them all manually? Is there a way to do this?
I suppose I should add it into the django base view class somewhere, but I am unsure exactly where to add it to in that?
Any ideas?
EDIT:
What I am trying to do is have translations set in my db under a certain model and then have the site default text areas be displayed in a certain language based on the url...
/eng/some/url
/esp/some/url
those two urls would display different languages, but I have to capture the variable and put it into each and every view which is quite cumbersome
Django already has some i18n support in urls, check it out. You need to activate django.middleware.locale.LocaleMiddleware by adding it to your settings.MIDDLEWARE_CLASSES and to tune your urlconf a bit by wrapping your urls with i18n_patterns.
The complete example is given in the docs, I see no sense copying it here.

How to add report section to the Django admin?

I want to implement a report section in Django admin. This would mean adding a custom section in the admin homepage where instead of a list of models I would see a list of reports. I want to use Django's admin tables with filters, sorting, everything if possible.
What would be the "best" way of achieving this? I realize this is a "big" question so I'm not asking for code snippets necessarily, a summary of needed actions would be just fine :)
P.S. Be report I mean a "made up" model by custom queries (queryset or how it's called).
P.S.2 Maybe this question should be something like: How to use Django admin tables functionality in own admin view?
P.S.3 Or maybe there is a way of providing to the existing admin interface my own data. This way I don't have to do anything else. I just want to say instead of a model take this data and display it in a nice table which I can sort, filter etc etc.
So you are attempting to add in new pages into the django admin.
This section explains to you exactly how you can do so - https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites
The basic idea is to add in new urls that you want in your urls.py as if you are adding urls for your "front end" pages. The key difference is that these new urls you are adding should start with ^admin/ and would look something like ^admin/my_special_link_in_admin and this url will point to your own custom view function at a location you so prefer.
E.g.
(r'^admin/my_special_link_in_admin/$', 'my_custom_admin_app.views.special_admin_page'),
So this is the way for complete customization. There's a very good tutorial which I refer to here - http://brandonkonkle.com/blog/2010/oct/4/django-admin-customization-examples/
In addition, if you don't want to do too much work, consider using Django Admin Plus - https://github.com/jsocol/django-adminplus
Or a django-admin-views - https://github.com/frankwiles/django-admin-views

What is the right way to organize a Django Project?

I'm new to Django and I'm trying to wrap my head around how these apps are supposed to be organized. I have the following questions:
Are apps like divs that are generated separately?
Can we have apps within apps?
Can we have apps that when clicked on, change other apps with javascript?
Right now I just have one views.py file and it loads all of its content through different function calls.
So right now I'm faced with if I should break up my views.py into smaller apps.
Am I going about Django the correct way?
Are apps defined like the they are in picture below, or are apps supposed to act more like a page?
What if I want a header, breadcrumbs, and footer for all my pages? I'm super confused #.#
Apps have nothing whatsoever to do with divs. Django is not a CMS (although it can be used to create CMSs) and doesn't dictate the layout of your templates.
The usual way to handle different blocks on the page that need different logic to populate them is via custom template tags. James Bennett has a good writeup on this, although the syntax is rather out of date so refer to the first link for that.

Flexible block positioning in Django?

In Drupal you could choose in which "region" of your site you want your block displayed. You did not have to modify any php/html code in order to achieve this.
Can such a thing be achieved with Django, and if yes, how?
By block I understand a piece of html output that doesn't have it's own URL and gets displayed along side the main data. (for example a search box or a poll)
Hm you probably want to create context processor and just output from it where you want it in template?
If you want reordering of content blocks in html output inside admin then you need something to generate that output like cms. You could try something like django-fluent-contents for this without requiring big cms.
Django and Drupal shouldn't be compared like this: Drupal is a CMS, Django is a web framework.
If you want to get a somewhat similar experience, I would look at using django-cms. With this, you can create numerous templates and set placeholders within these templates (these are regions of the page like 'sidebar', 'footer', 'content area' etc.). When you go to create a new page in django-cms, you select which template you want to use (maybe a two column layout or a three column layout with a header - depending on what you have created) and then you choose what content (or plugins) you want to place within the placeholders you have created in the template. So this is a somewhat similar experience to Drupal's regions.

How to get page parent when using Django flatpages?

Since Django urls are arbitrary, including those set when you create flatpages. How can you figure out what a page's parent is? That is if I create a page /about/contact/, then when I am on the contact page (in the template), how can I figure out what the parent page is?
Is there a standard way to do this? Or do I just split the slug on the slashes and use the first section?
Basically I'm trying to figure out how to create different site sections.
As far as I know, django doesn't have a native concept of sections and, as you said, the URLs are arbitrary (they're left to the programmer to decide). If you stick with the same URL structure throughout your whole site (/<section>/page) then you can infer the section from the URL.
I'd rather set the section in the url patterns and pass it to the views (as described in here) which in turn can pass it to the templates...
flatpages are essentially the same thing as static html pages, stored in your database with a pretty admin in front of it.
so just hard code a link, it shouldn't be a big deal.
if it is a big deal, that means flatpages probably isn't the solution for you