flask WTForm: "working outside of application context" - python

I'm trying to extract the values of a Select field, from a JSON file that houses the translations. The problem is caused by the get_locale() function, which can only be called within 'context.'
This is the form select-field:
brand = SelectField(choices=generate_brands(get_locale()),validators=[Optional()])
is there a way to load this specific field only when called inside of a view when the request variable is available?

If I well understood, you want to populate the SelectField depending of the language of the user. There are several ways to have dynamic SelectField, see Oleg's answer for a good example: https://stackoverflow.com/a/48236887/11405279

Related

How to store user specific data in Django

In Django,
How can we store user specific data?
I'm trying to creating a django web app, where user needs to remember the order of random sequence fetched from database.
I have two functions in my views, home and result
I tried to store random sequence generated by home function into a global variable, and use that to validate in results function.
But , I think this can serve only one user at time
What if second user requested for home page and that cause change in global variable, resulting unable to validate first user.
You can tackle the problem with Django Session.
In home view you can set variables by :
request.session['varibale_name'] = varibale_value
You can access these variables in result view by :
request.session['varibale_name']
After job done you can also delete the session variable by :
del request.session['varibale_name']

Django: modifying data with user input through custom template tag?

Is it possible to modify data through custom template tag in Django? More specifically, I have a model named Shift whose data I want to display in a calendar form. I figured using a custom inclusion tag is the best way to go about it, but I also want users to be able to click on a shift and buy/sell the shift (thus modifying the database). My guess is that you can't do this with an inclusion tag, but if I were to write a different type of custom template tag from the ground up, would this be possible? If so, can you direct me to a few resources that address how to write such a tag?
Thank you in advance.
This type of logic does not belong in a template tag. It belongs in a view that will respond to AJAX requests and return a JSONResponse. You'll need some javascript to handle making the request based on the input as well.

Newfies-dialer Call Reports module

I have newfies dialer project, i would like to add a new field in Call Reports section.
I have lot of field that are unused in contact model. One of them can be used for this special id for my purpose.
How can i do that? Please help me anyone who familiar with newfies-dialer.
Newfies-Dialer is based on the Django framework, so it's good to know about Django to hack on the project.
You will notice in Newfies-Dialer that there is a template called: dialer_cdr/templates/dialer_cdr/voipcall_report.html
in which we display the data that is passed from the view.
So then in dialer_cdr/views.py, you have a view function which is in charge of rendering template and pass it some data. There, you can either modify voipcall_list object to add extra data to it, like info from the contact model, or pass an other object to data.
Here a link to the function handling this view: https://github.com/Star2Billing/newfies-dialer/blob/v2.12.2/newfies/dialer_cdr/views.py#L169

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.

How can I pass a variable for a jinja macro from the controller?

In a controller (//view) I perform an exists operation on the database.
If the result is positive, I return the same URL and the form and flash a message.
When doing this, I'd like to render one of the fields (ie. form.email) with an extra variable set (that normally defaults to None) in order mark it as an erring field.
How can I do this using jinja templates and flask?
I am not asking for code but direction as I am unable to figure out the way to go.
I am working with Flask, WTForms and Jinja.
Thank you
If your procedure encounters a (high level) operational error and you can not declare this as a validator within your WTForm objects, you might want to find another solution to mark that specific field as erring.
One way to do this can be using WTForms' helper class flags.
Ex:
form.field_name.flags.erring = True
Then when you are rendering your field, via a macro or not, you can check to see if erring flag is set to True.

Categories

Resources