Web2Py - Custom File Upload Form with Crud - python

I'm creating a custom file upload form in Web2Py, and was hoping some more experienced users could help me solve a few issues. Basically, the database ("t_file") is defined in "db_wizard.py", and in the controller, I'm calling crud.create(db.t_file, next=URL('upload')); a form is added in the html file with {{form}}.
There are about a dozen fields created, two of which are selectors, one is a file upload/browse field, and the rest are input boxes. I would like to make the following changes:
-Currently, the selectors default to an empty option. They are defined in the DB file like this:
Field('f_data_real_or_fabricated_bool', 'list:string', requires=IS_IN_SET(['T','F']),
label=T('Real or Fabricated')),
However. when displayed, the first option is empty, and the two other options are below the empty option. Is there a way to get rid of the empty option?
-The regular text input boxes, the selector boxes, and the filename input box are different widths. What is the best way to make them the same width? I've been trying all sorts of things with the CSS, but can't seem to get it.
-Is there a way to use expandable text boxes for some of text input areas?
-I would like the first several input fields to be required, and the rest to be optional. The mandatory fields should appear on the upload page by default, and the rest optional fields should only appear when an "advanced fields" (something along those lines) checkbox is checked. What is the best way to do this?
Can the above changes be made by sticking with the crud.create or crud.* methods, without designing custom forms?

I think you should attack one thing at the time and, for each one, try paste some code that helps a lot. I try to answer the last question: I think you won't be able to do that with crud interface without change the inner code (DO NOT DO THAT!). With SQLFORM, you can, altering CSS on Fields. But, the best and more controllable, although more hard work, is creating custom forms.

Related

Django modelform: Create new related object

I've been searching stack overflow and google for a solution for over an hour now, and I can't seem to find something that, in my opinion, should be easy to obtain (as it's a common use case).
I've checked this thread, and a few others, but I haven't been able to find a real, easy solution:
Django modelform: is inline adding related model possible?
Anyway, say I have a model with three related entities, two foreign keys and a many-to-many related class. Now, I have a ModelForm which displays these in comboboxes and lists, but what I need is that "+" button next to these elements (as seen in the admin interface).
I want the plus to take me to a new form, for that particular entity, allow me to submit the new information, create the database entry, take me back to my original form and have the newly added entity selected in the combobox. I'm really hoping the django ModelForm Meta class has an attribute that I can't seem to find which enables exactly this.
This isn't really a django question.
This has to do with presentation of a particular widget in an html document, and that is governed by either the HTML markup, CSS, or javascript.
Django is a server side application and is primarily responsible for creating a valid http response and receiving a valid http request (of course, there is a lot that happens in the interim and that is why django is so big) but it's not a "one toolkit to kill them all" app.
I think you want to look at bootstrap: http://getbootstrap.com/
Jquery UI: http://jqueryui.com/
Or some combination of the two.
You can also just mark up the document yourself with a stock img or something.
However, if you want to do it exactly how the admin does it, just go into django.contrib.admin and examin the code to figure out how the django developers did it. I believe they are just using Jquery UI and some manual markup to accomplish that.

Django Clear All Admin List Filters

I have a Django admin control panel and in each list of objects there are lots and lots of list filters. I want to be able to clear all the filters with a click of a button, but can't find where this ability is, if it already exists in Django.
Routes I'm considering (but cannot figure out):
Make the last item in the breadcrumb link to the full list
Make a direct hyperlink as a filter list option
Find some way to access all the query options and remove them or simply return a blank one (queryset.all() isn't working; I'm probably barking up the wrong tree.)
That kind of thing should already exist! Find out how to use it.
Does anybody know how to accomplish this? I've been trying to figure it out all day.
If you have at least one entry in search_fields and therefore are showing a search box on your admin changelist page, if you have any filters or search terms in effect you should see information to the right of it showing the number of rows that match your current filter and search criteria. It'll be worded as something like "5 results (50 total)". The "50 total" text will be a link to an unfiltered version of the list, showing the whole set. Possibly paginated, but all filters will be cleared.
This doesn't appear to be automatically exposed without the search box. The filter settings are simple arguments in the URL querystring, so it should be easy to add a link similar to the one in the search box that just drops the querystring, but you'd have to learn a little about the admin templates to do so. Setting a search_fields entry is probably simpler, if you have anything reasonable to search over.

Custom Django Database Frontend

I have been trying to get my head around Django over the last week or two. Its slowly starting to make some sense and I am really liking it.
My goal is to replace a fairly messy excel spreadsheet with a database and frontend for my users. This would involve pulling the data out of a table, presenting it in a web tabular format, and allowing changes to be made through text fields and drop down menus, with a simple update button that will update all changes to the DB.
My question is, will the built in Django Forms functionality be the best solution? Or would I create some sort of for loop for my objects and wrap them around html form syntax in my template? I'm just not too sure how to approach the solution.
Apologies if this seems like an simple question, I just feel like there is maybe a few ways to do it but maybe there is one perfect way.
Thanks
The fastest way not to implement you own pages and to have a tabular view of your data is to use the django's built-in admin interface. It gives you sorting, filtering and search functionality and quick to start. You just need to define your models in models.py and setup the admin pages as described in the docs.
Normally the admin page is not used as a representation to users or customers but in the case you described it seems a clean and quick choice.
Exporting the excel sheet in Django and have the them rendered as text fields , is not as easy as 2 step process.
you need to know how Django works.
First you need to export the data in mysql in database using either some language or some ready made tools.
Then you need to make a Model for that table and then you can use Django admin to edit them

Scalable Widget for Rendering Foreign Keys in Django Admin

Is there a widget for rendering a foreign key field in Django admin that can scale to handle an arbitrarily large table and provide a user-friendly interface for looking up a FK?
The default widget in Django 1.3 is a select box that lists every record in the referenced table. For tables containing hundreds of thousands of records...this basically crashes the server. I see there's a ref_id_fields ModelAdmin option, for rendering the field as a simple text box, but that strikes me as a pretty un-user-friendly workaround since it expects the user to know the exact ID for the record they want to reference.
I'm surprised no solution for this is already builtin, but Googling only found me one project implementing a jQuery-powered autocomplete widget. Are there any other solutions for this problem?
are you getting all the static files? raw_id_fields come with an ajax widget, which puts a little magnifying glass next to the text box with the input widget. clicking brings up a popup with a changelist (including e.g. search) that you can use to find the id. (and automatically insert by clicking the entry)
see screenshot (first google image search)
There are several more auto-complete apps for Django: http://djangopackages.com/grids/g/auto-complete/

Display custom labels for values in the Django Admin Site

One of my models has a 'status' field which is only ever modified in code. It is an integer from 1 to 6 (although this may change in the future).
However, in the Admin site, I would like to display a label for this data. So, instead of displaying '5', I would like it to say 'Error'. This means I would be able to easily filter the objects in the database that have the status 'Error' and also my colleagues who don't know what each status means as they are not involved in coding, can use the admin site to its full.
I don't know if I am going the right way about this or if it is even possible, but I would appreciate any help you can give. I would rather not change how the status is stored as it would require a big re-write of some parts of our system. In hindsight I guess it was a bad way to do it, but I didn't think the field would matter as much as it does.
Consider to use choices. Anyway you can customize lots of things in django-admin, just read the docs:
http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
You could create a custom field type that overrides to_python and get_prep_value to store integers in the db but use string values within Python:
http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.django.db.models.Field

Categories

Resources