Django CMS dynamic templates forlder for mobile - django

I have a Django CMS 2.4 website and currently when detecting mobile user agent I switch to a different URL settings and serving different pages for mobile. Now I would like to use same content for both but to use different template for mobile and desktop versions so I don't need to do edits in two places and so the URL will stay the same for both. I know it would be easy to do in Django, but having Django CMS I have no control over rendering and template selection. Can I do it in any way, or should I look for a different solution?
Thank you.

Django CMS will check if there is an template variable in POST or GET in request, and if it's a valid template (it's set up in CMS_TEMPLATES in options.py), it will be used to display page.
The following snippet can be used in middleware.py for switching templates:
if request.COOKIES.get("mobi")=='y':
if request.current_page:
request.POST._mutable = True
request.POST['template'] = 'mobile/' + get_template_from_request(request)
request.POST._mutable = False
One thing you need to remember, this will only switch templates for page, to change a template for plugin you need to modify instance.template variable in render method of plugin.

Related

For development purpose, how can I open a Django Jinjia2 template in browser with preliminary rendering (extending, including)?

Problem description
I am starting working on a Django project and the server-side rendering template is a little hard to work with. Usually I develop the front-end application with hot module reload server so that I can see the rendered page during development.
However, for Django project I can only view the site by serving and open from browser. If I open the template directly, the include, extends are not processed, hence CSS and JavaScript are not loaded in the base template.
My question is, is there any tool or workflow that can help develop the Django template in offline so that the style and layout can be rendered properly during development?
Edit: justification
As comment mentioned there are some plugins that supports reload the Django page. However, I would like to know whether it is possible to work with the template HTML totally off the Django server, i.e. work with the html static page? There are some scenarios where I feel it is not suitable:
A page that refreshes slowly: e.g., slow database query before the page can be rendered.
A template that is not accessible normally: e.g., a part of html inside {% if %} that is not normally accessible, such as an error message.
A template that is not yet registered in the urlpatterns routes.
Thank you

Adding Wagtail to an existing Django app

I'm quite new to Django and Wagtail, and I'm having some difficulty with what I think is a very basic use.
How do I allow Wagtail to edit an existing view's template, while serving that template using Django's serving mechanism?
Assume I have an app (HomePage) created to serve the site's main index (/). I have the HomePage's views set up to render template and certain elements dynamically. Now I want that template to be editable via Wagtail's CMS interface. Something as simple as an image on the frontpage, or a headline.
The closest I've gotten so far has been to follow the Wagtail beginner's tutorial to override the base HomePage class in my app's models.py. That only made my pages available via the /pages/ URL.
Thank you for any help.
Since your site's home page is not a Page object in the Wagtail sense, I'd suggest looking at Wagtail's facilities for managing non-page content - snippets and ModelAdmin would be possible candidates, but I reckon the site settings module would be the best fit.
A Setting model gives you a set of fields which can be configured for display in the Wagtail admin using a 'panels' definition, just like you'd get for a page model - with the important property that only one settings record exists per site. You can retrieve this record within your homepage view or template as shown in the docs, and output it on your template as desired.
One way do that, is to let Wagtail serve your homepage. You will need to change your project's url configuration accordingly, to make wagtail's urls serve the root of your site.
Then, you can pack your dynamic content into a custom template_tag and include in your homepage html template.

Changing from Django form to ReactJS

Currently I have a django sign up form rendered with a django template. I'm starting to switch to ReactJS so I wanted to change that form to a Modal window rendered with react.
The modal will replace the form so it will contain a form in its body containing the same input fields as the django form, before starting i need to know if the modal will work the same as the form or do I need to do some changes like binding it to the django view.
I'm using this setup now for the frontend of one project. This is far from trivial and took me some days to figure out. Start with this webpack-django library. You will need to setup your paths carefully along the road. Also for decent development experience, you will need to run webpack in watch mode as the rendering is dealt by django server, not webpack-dev-server (I run this mode in the start script of packages.json). After you can make this work, you will have some obstacles like: csrf tokens and confilct of urls.py with react-routing.
Well, I'm not being more speficic because not sure if you want to follow down this road. Also it requires a tutorial on setting up npm, webpack,django that would raise tons of questions. Besides, I would not recommend doing this just for a login form. You can give up on django forms and build your modal in vanilla javascript/html/css using django's template engine and bootstrap.
Hope this helps!

Integrate existing blog code into Django-CMS?

If I already have a blog app done with Django and I want to use it with my new Django CMS site, is it okay to simply drop it into my new Django CMS project as a decoupled app and match anything /blog/ to the blog app as apposed to a Django CMS plugin? I guess what I need to know is when is it best to write my Django app as a plugin vs an entire app?
Thx
JeffC
Yes, but you don't just drop it into the urls.py, instead you can write an AppHook to tie your blog's URL scheme to a particular page in your CMS.
Plugins on the other hand are useful if you want to inserts particular aspects of you app into other page's placeholders - for example to show your latest 3 posts on the frontpage.
You might also want to include your blog's paths in a breadcrumb or menu on your site - in that case you need to write a custom Menu too.
Finally, it might also be useful to make use of django cms's placeholders in you blog model. His would allow you to post a variety of content via plugins.

Modifying the django-cms 'add plugin' template on a per plugin basis?

The template I'm specifically talking about is the one that is used when a user add's a plugin to a page. Both in the admin area, and when modifying pages directly, it is displayed using an iframe.
The template itself is located cms/templates/admin/cms/page/plugin_change_form.html.
My problem is that I need some javascript to populate a drop down list within the form. All the javascript is run before the iframe is added to the page though, so I thought if I managed to edit the template I can tell the iframe to load some specific js. I can obviously just change the template directly, but that's a bit of an undesirable solution. I would rather keep it within the django application and even better have the js run only on specific plugins.
Any thoughts are appreciated.
You can set the change_form_template on your CMSPluginBase subclass, as CMSPluginBase is a subclass of django.contrib.admin.options.ModelAdmin.