How to change the SQLFORM style in web2py views - python

I would like to have a custom view of login and register forms in my views, without loosing the functionalities of SQLFORM. How can I change the "look" of the forms in views. Please give me some pointers like which CSS file I should be modifying. I donot have knowledge on HTML and CSS, hence I am asking your help.
Thank you so much
A thinker :)

First, you can alter the general HTML structure of forms by using the formstyle argument to SQLFORM. It takes values, "table3cols", "table2cols", "divs", "ul", "bootstrap", "bootstrap3_stacked", and "bootstrap3_inline", and it can also be a function that generates a custom structure (for an example, see the gluon.sqlhtml.formstyle_bootstrap3_stacked function). For Auth forms, you can set the formstyle via the auth.settings.formstyle setting (see here).
You can modify the CSS for forms however you like. Look here for relevant details. You may also find it useful to generate a form and then examine the generated HTML source code in your browser to understand the structure of the form. If you are using the "welcome" app as your foundation, the relevant CSS files that currently impact form appearance are skeleton.css and web2py.css in /static/css (as of version 1.99.3, which was just released today).
If you need to do more than can be handled via the formstyle option and CSS changes, then you can create a completely custom form in HTML. See here and here for details.
If you have specific questions as you proceed, feel free to ask for help on the mailing list.

Related

Django Editable Text Field

I've been able to easily create a list of editable items and manage them through the Django admin panel. That seems pretty simple and I have a good idea of how models work from other frameworks.
Although, I'm curious to know to know how I can make something as simple as editing a text area on a static site. Basically, I don't need to "add post" or anything of the sort. I just want to be able to edit a text area on a static site.
Any docs or examples?
I've been looking at packages such as django-flatblocks and chunks, these seem to possibly help with what I'd like to do. I'm just new to the Python world so some of this stuff is a little magic to me, especially involving the administration panel.
One thing that I realized is that these libraries (or apps in django?) seem to be super out of date, stemming back to the last commit being from 2012. Are these still commonly used?
Figured out a solution in case anyone is interested. I ended up using a Django application called django-generic-flatblocks which seems to provide me with what I need. Although, it's a bit strange as I had to replace all the text on my site with a gblock and then re-enter it all. Seems as if upon first creation of a block, it's empty so you essentially have to provide it with a value.
After that, if you log into the admin panel you're able to pull up the block and edit it. Alternatively, if you're logged into the Admin panel you're able to view your site and an edit tag is provided and you'll go directly to that block in the admin panel.
If anyone knows of anything which essentially allows me to define the text blocks up front in the admin panel then add the tags to the code, please lmk. I'd prefer to load all my content into the admin panel first and then just throw the tag into the code and have it display. That would save a lot of time in terms of having to copy the existing content, store it away, adding the tag, and then having to put it back in.
This seemed to be the only one of the recommended apps that worked for me. I tried to use Chunks because this really is only for title/text but on Django 1.11 it would freak out on me about not having South.db, which isn't even used.
https://github.com/bartTC/django-generic-flatblocks

Creating reusable forms/views in Flask

I am not sure what would be the best route to go down, or I may be missing something obvious.
Example I can give is I have 'person' model and associated form, and view created to add a new 'person'. This all works great. What I would like to do though is use this 'view/form' in a master page with other similar 'views/forms'. With each part being able to add/edit or delete a record from each sub view/form.
So I have all functionality done, just don't know how I can create this master page with child objects, but these child objects can be their own page as well, type of thing.
The idea being that the master page structure is flexible and can accommodate various elements based on the context the user is in.
Should I be looking at blueprints or Jinja2 and its template structure. Or is it how I am handling routes within the main app.
Apologies if this is too vague.
I have done this using AngularJS ng-include directive. You can include whatever html you want, but be careful with Jinja2. If the html you are trying to include contains any script tag it will crash. See my question here. If you need to import a form that needs a script tag you will need to make sure it is not loaded when you are pushing it with Angular. Since it makes a xhr request, you can use flask.request.is_xhr to check if it is angular or the user that is requiring the form. You cannot forget to add this to your angular app
Otherwise is_xhr will always return false.
myAppModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
Be careful with base_ templates as well, since they usually load script tags. You can pass the base template through your flask route, and make it extend a blank base html when the request is made through angular. I wish I had my example here, but I am on the bus. Let me know how far you got.

How a django form is passed to a html template

I am new in Django framework and trying to understand how it works and what is its structure. I am just curious about how a django form is passed to a html template ? Any help would be appriciated.
I think you are curious in how django converts a python object into HTML, its internal mechanism, so the tutorial might not cover what it is.
I did have the same question before, but if you do look at code in forms.py https://github.com/django/django/blob/master/django/forms/forms.py you will see that internally there are methods which will look at the attributes of the objects that you have declared and generate snipplets of html code which will then be rendered.
Of course I cant tell you exactly how it works, I leave the heavy lifting to django.. Isn't that why we use a modern web framework like django in the first place.
Hope you will find this useful

Is it possible to generate static html from Django Cms?

Hi I'm working on a project based on Django Cms
DJango Cms
Most of the templates ara generated via an APi call in ajax.. I wondered if is possible to generate an HTML static file from the original template file in order to avoid dynamic calls.
Short answer: Yes.
Long answer: It depends.
There's little stopping you from just creating static HTML pages (you could for example just use wget to crawl you website. However note that this only works if your content is not dynamic, as in, it doesn't depend on whether a user is logged in or not etc. If you only use plugins that always have the same output, regardless of the request, then it'll work.
Since Django CMS gives you a lot of power to write highly dynamic plugins, there's no built-in way of generating these static pages (the chances of someone using it without realizing the drawbacks are high).

Django - Managing page content in Django Admin

I'm new to working with Django and am developing for a client who wants to be able to change page content in the Django Admin. They need to be able to change the html of the index page without editing the files on the server.
I know about flatfiles but I'm not sure that's completely what I'm after as I can't display stuff such as Django forms for example.
EDIT: Kind of like how a CMS works but without the users/group stuff and be able to use Django View modules such as forms.
Any advice?
Thanks
Honestly, the scope of what you're looking for is too huge to cover in this format. There's a number of ways this could be done, but they're all going to require some work and customization based on the client's needs.
Flatpages could work if you allow HTML content and make sure the content is rendered as "safe" in the template. This really only covers the "content" area of the site, though. It wouldn't be wise to use flatpages for an entire site template, including header, sidebar, footer, etc.
You could create editable areas. So, you actually create models for things like headers, sidebars, footers, and modules within those areas, and then just pull them into the template as needed. Then, the client is only editing pieces of the template instead of responsible for the whole HTML document.
Forms are going to be a challenge, because they require backend-processing that requires a connected view. The client won't be able to just arbitrarily drop in some form code and have a form. But, you could use a third-party service form forms and just embed them in the available content regions. Or, there's a couple of django apps that try to implement a type of "form builder" in the admin. That might somehow let the client add a form via something like the shortcodes used in Wordpress, but you'd likely have to lay down some infrastructure to make that work.
At a certain point, stuff like this reaches a point of diminishing returns, though. The only way to allow total customization of the template is to drop down into the actual physical file and make changes there. You can make certain things easier for the client, but ultimately, they either need to scale back their customization needs or deal with the fact that they'll have to work with the filesystem.
I don't believe that is possible at this time. Of course you can edit your models but templates, I think not.
I would find out how much they need to change? If they plan a complete redesign every week then you're still looking for an answer. If they just need a dynamic front page then you can split it up into variables and let them edit sections of html. Much safer and less prone to breaking the html.

Categories