Django: How to populate value into form template? [closed] - django

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have created a small form which includes something like name, mobile number, age field. All are text fields.
These values are stored in DB.
I want my template to pre-populate the form with values from DB (if exists). How can this be done?
My template code is as below:
<div>
<center><form action="/profilesettings/" method="post" enctype="multipart/form-data">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="submit">
</form></center></div>
Currently, it displays form with empty field. Instead of empty fields, I would like them to show values which are fetched from DB.

check this example on django docs
article = Article.objects.get(pk=1)
article.headline
'My headline'
form = ArticleForm(instance=article)
form['headline'].value()
'Initial headline'

Related

Flask-Admin - what to pass in url_for(' ')? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I got an error, that says, that no URL can be build from that parameter inside url_for('').
I passed url_for('admin') but that does not work. What should I pass to get to localhost:5000/admin ?
Thank you
You pass the name of the definition of the route you want to visit.
For example:
#app.route('/')
def index():
return 'Hello, World!'
...
#app.route('/some_page')
def somewhere_else():
if something_happened:
redirect(url_for('index'))
return render_template('some_page.html')
Or if you are using it inside your HTML:
<a href="{{ url_for('index') }} ...></a>

How to use a variable in views.py inside a template in Django? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a variable in the file views.py of my app directory.
I need to print that variable inside my template.
How can I do That?
You can pass the variable into context dictionary and render it in the template,
When you use a Django Template, it is compiled once (and only once) and stored for future use, as an optimization. A template can have variable names in double curly braces, such as {{ myvar1 }}, {{ myvar2 }}.
A Context is a dictionary with variable names as the "key" and their values as the "value". Hence if your context for the above template looks like: {myvar1: 101, myvar2: 102}, when you pass this context to the template render method, {{ myvar1 }} would be replaced with 101 and {{ myvar2 }} with 102 in your template. This is a simplistic example, but really a Context object is the "Context" in which the template is being rendered.
As for a ContextProcessor, that is a slightly advanced concept. You can have in your settings.py file listed a few Context Processors which take in an HttpRequest object and return a dictionary (similar to the Context object above). The dictionary (context) returned by the Context Processor is merged into the context passed in by you (the user) by Django.
A use case for a Context Processor is when you always want to insert certain variables inside your template (for example the location of the user could be a candidate). Instead of writing code to insert it in each view, you could simply write a context processor for it and add it to the TEMPLATE_CONTEXT_PROCESSORS settings in settings.py.
An example,
In your views.py
foo = 'bar'
context = {'foo': foo }
return render(request, 'template_name', context)
And in your template, you can access the variable,
{{ foo }}

In a Flask app, how to print each item of a list in the new paragraphs inside my HTML page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm building a Flask app which is supposed to return all the items in a list as a new line in the HTML page.
For example:
list = [1,2,3,4]
I want to print each item in list as a new paragraph in my HTML page, like here:
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
You should better follow the flaskr tutorial on flask web site. It can give you the idea how to pass local variables to the template.
#app.route('/')
def your_view():
your_list= [1,2,3,4]
return render_template('your_view.html', your_list=your_list)
then in your jinja template, iterate over this list.
{% for your_list_element in your_list %}
<p>{{ your_list_element }} </p>
{% endfor %}

HTML5 - simple pattern don't works with Smarty [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm using a simple pattern like this :
<input type="text" pattern="[a-z]{2}" required ../>
But it's never valid. It seems it didn't works.
I tested it in Firefox
Is there something to active or something like that ?
My template :
<section class="inscription">
<h1>Inscription</h1>
<form method="post" enctype="multipart/form-data">
<div class="formu-inscription">
<label for="nom"> Nom : </label> <br/><input type="text" id="nom" name="nom" value="{set_value('nom')}" pattern="[a-z]{2}" required /> <br />
{form_error('nom')}
/* others inputs */
For people using smarty template, you can do
pattern="[a-z]{literal}{2}{/literal}"
Looking at the rendered source code you posted as a comment on a now deleted answer you have
<input type="text" id="nom" name="nom" value="" pattern="[a-z]2" required />
This is not the pattern in your source that you posted in your question. It seems that you are using some unspecified templating system that is using the {} characters as field identifiers and is misinterpreting your pattern.
The result in your rendered page is the pattern [a-z]2, which will validate for a string like a2 or f2, but not a, or aa, a3 or anything longer.
Since you haven't specified what templating system you're using it's not possible to indicate how you might work around this. Possibly a pattern of [a-z]{{2}} might work.

ColdFusion - how to edit a record? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to edit the Record2 but when I click the Edit button it always display the info of Record1. If I click any other Edit buttons it only display the info of Record1. How does it know which record I want to edit? Please help. Thank you.
<cfform name="formName" action="edit.cfm" method="post">
....some additional codes.....
<cfloop query="qryName">
Record1_data Edit button
Record2_data Edit button
Record3_data Edit button
Record4_data Edit button
</cfloop>
....some additional codes.....
</cfform>
Unless there's a really good reason I'd really shy away from using cfform there's rarely a good reason to use it
You need to pass in some sort of form variable that has the corresponding ID to what you're pulling into the database.
<form name="formName" action="edit.cfm" method="post">
<cfloop query="qryName">
<input type="checkbox" name="Record" value="#qryName.ID#" /> Record #qryName.ID#
</cfloop>
</form>