I am new to python and flask and trying to understand "wtforms" but so far didn't find any tutorials that actually explains the complete functionality. Like in some places data is set in the "GET" request like
form.country = DBModel.country
some use it like
form.country.data = DBModel.country
other use it like
form.country.query = DBModel.country
The other thing I don't understand is "wtforms" functionality in "POST" request.
Where exactly data is extracted from the request?
I cannot see any data extraction from request in any example. They simply define the instance of "wtform" and starts adding the data to DB.
If You are new in Python and Flask read this blog :) It contains series of articles how to start and develop the flask project. It can be very useful.
About problems with wtforms try to find something useful in flask snippets.
In wtforms.Form you have properties data - it is a dict containing the data for each field. If You want to fill form with data from model there is populate_obj() method.
Related
I'm new to django and still trying to figure out logic. I watched a bunch of videos and I know how to create those examples but I'm not quite there. I'll explain what I want and maybe someone can explain to me a logic how to do, yes?
I created an app and inside an app I connected urls, settings, html and I have working site. Now I want to input data in django website, django to pass data to xlsxwriter script to create xlsx document, and to pass that document to user to download.
My logic: What I need now, is to create some kind forms (but not for registered users, it can use anyone who uses a website) to input data. Which forms are best? Than I need to pass () that data to function that are in views that will get an arguments from html page through some kind of forms, create document, and pass that to download button.
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
I have database created by django, but I can't print a text in my page from database. What function I will learn to make that.
You should have elaborated your question with your code samples. Before diving deep into any framework, you should go with tutorials. Django website itself provides you a step by step tutorial for that.
Here are few references:
https://www.youtube.com/watch?v=MEcXbeY4_DQ
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
You will need to read the database in respective view function called in views.py in your app; and return the data as context to template so that you will be able to access them and print them.
I have RESTFul API written in pyramid. View functions processes data in request.POST and request.matchdict and returns json response.
Eg: A method inside view class.
#view_config(route_name="temp_name", request_method="PUT")
def put_item(self):
# validates and processes self.request.POST
# validates and processes self.request.matchdict
# returns json reponse
As you can see, I'm doing validation inside view method, which I want to avoid.My intention is to separate validation from actual functionality.
How do I handle this?
I saw colander http://cornice.readthedocs.org/en/latest/validation.html#using-colander which looks really good in my case. But looks like it is integrated with cornice which I'm not using at all. And also, I can't convert whole app into cornice now. Is it possible to use colander in the same way as given in the above link with my app?
This is the first time I'm writing RESTFul API's, also just started learning pyramid and colander. Need your help. Thanks in advance.
You can use Colander independently of cornice. The most basic example for using Colander Schema in a pyramid application I remember you find here:
http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/humans/zodb/index.html
This way you can encapsulate schema validation using colander schemas and validators.
A more recent introduction of pyramid 1.5 branch into that topic you find here:
http://pyramid.readthedocs.org/en/1.5-branch/quick_tutorial/forms.html
Oh, and look at that SO question. I liked it, may be it will be helpful to you as well:
Which one is the correct approach for form validation ? Colander's Schema validation or Deform's form validation?
So im trying to use my django installation to create a dashboard a combination of all the data from the 4 other models and views. For our use of django we mainly use it for stats so it's generally just pulling numbers out onto the main index page. Right now I have my index template set up as a redirect_to_template and it goes straight to a template (since everything is still static). Im trying to figure out if im going to have to create another app and pull in all the data to a new view & model for this dashboard page, or if I should create sub-templates if that would work to pull the data.
Thanks again!
I think you're better of with pulling the data by using ajax from your dashboard, it will be a better UX when you have a lot of data to fetch. For that you can use one of the known 3rd party apps for creating REST API or change your existing views to deliver json response as well.
To help anyone that has the question in the future. I ended up importing the apps such as
from status import Alert
context['list'] = Alert.objects.filter(All My Filters Here! (link below))[:8]
Then just for loop list in the template!
https://docs.djangoproject.com/en/dev/ref/models/querysets/
Hope this is helpful to someone else!