The 'image' attribute has no file associated with it django - python

I am doing an ecommerce project for deployment in pythonanywhere.com, some error is coming
I would really appreciate if any one could help me to find out the problem as I am a basic learner
TIA
I have developed a online book store application with python and django,I have two MySQL tables for category and products , while running in local host it works perfectly, but the deployment in pythonanywhere only got problem in images field, also static path given
errorenter image description here
enter image description here

Need to Check image is not None with if condition
<table>
<thead>
<tr>
<th>First Name</th>
<th>last Name</th>
<th>Mobile</th>
<th>E-Mail</th>
<th>City</th>
</tr>
</thead>
<tbody>
{% for i in data %}
<tr>
<td>
{% if i.image %} # need to check image is not None, this if check if image then render it else not
{{i.image.url}}
{% endif %}
</td>
<td>{{i.first_name}}</td>
<td>{{i.lastname}}</td>
<td>{{i.mobile}}</td>
<td>{{i.email}}</td>
<td>{{i.city}}</td>
</tr>
{% endfor %}
</tbody>
</table>

I found that the problem was any of the image field was none, so I checked from the admin panel all the records and got one row with image attribute empty. So after adding image it works perfectly.

Related

json as table with django

I have modeled my database using models.py within my django project, one of the fields is a JSONField and I can save json data into that field without any problem. My doubt comes in how I can show that information as an html table. At the moment I have been using ListView to show that information in a template but I don't know how to transform it into a table.
If you use object.json_field.items you can loop through them just like a normal dictonary / can also use .keys and .values
Example use in a table
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for k, v in object.json_field.items %}
<tr>
<th>{{k}}</th>
<th>{{v}}</th>
</tr>
{% endfor %}
</tbody>
</table>

Django: Transfer multiple data into next template

Good day,
I'm asking myself, if it's possible to transfer multiple informations as primary-keys inside my template!?
For example, when clicking on a link inside my table...
In this case im transfering the item-id:
<tbody>
{% for item in dataset %}
<tr>
<td>
Item-Name
</td>
</tr>
{% endfor %}
</tobdy>
Now I want to transfer the id and - let's say - the name! Is something like this even possible?
<tbody>
{% for item in dataset %}
<tr>
<td>
Item-Name
</td>
</tr>
{% endfor %}
</tobdy>
And if it's possible, do I have to chage something inside my urls.py? Right now it's looking like this:
path('index/<str:pk>', views.example, name="Example"),
Thanks for all your help and a great day!
Your url should be
{% url 'Examplepage' id=item.id name=item.name %}"
And your path should be
path('index/<str:id>/<str:name>/', views.example, name="Example"),

How to display database on web framework?

I want to display the content of the database from .db file on web framework using flask module. However, only the row title is able to be displayed on the web framework. The content of the database from the .db file couldn't load out on the web framework. Anyone can help me with this? Thanks.
This is my code:
from flask import Flask, render_template
import sqlite3
app = Flask(__name__)
def connect_db(db):
con = sqlite3.connect(db)
return con.cursor()
#app.route('/')
def index():
db ='mcu_aurix_git.db'
cur = connect_db(db)
cur.execute("SELECT * FROM mcu_aurix")
data = cur.fetchall()
return render_template('flask.html', rows=data)
if __name__ == "__main__":
app.run(debug=True)
flask.html:
<table class="table table-hover">
<thead>
<tr>
<th>project</th>
<th>branch</th>
<th>id</th>
<th>number</th>
<th>subject</th>
<th>owner_name</th>
<th>owner_email</th>
<th>owner_username</th>
<th>url</th>
<th>commitMessage</th>
<th>createdOn</th>
<th>lastUpdated</th>
<th>open</th>
<th>status</th>
<th>current_date</th>
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
<td>{{row.project_name}}</td>
<td>{{row.branch_id}}</td>
<td>{{row.id_id}}</td>
<td>{{row.num_number}}</td>
<td>{{row.subject_name}}</td>
<td>{{row.owner_name}}</td>
<td>{{row.owner_email}}</td>
<td>{{row.owner_username}}</td>
<td>{{row.url_name}}</td>
<td>{{row.commitMessage_name}}</td>
<td>{{row.num_createdOn}}</td>
<td>{{row.num_lastUpdated}}</td>
<td>{{row.num_open}}</td>
<td>{{row.status_name}}</td>
<td>{{row.current_date}}</td>
</tr>
{% endfor %}
</tbody>
</table>
Is there anything missing in my code? Hopefully anyone can help me on this. Thanks in advance!
You are not passing rows variable to the html page.
return render_template('flask.html', data=data)
You are only passing data variable.
If you want to use rows inside your html page, you need to use
return render_template('flask.html', rows=data)
Also one more thing,
{{row.project_name}}
You cannot get the value of project_name like this, you need to use index value (col. no. starting from 0). Like,
{{row[0]}}
Instead of manually creating <td> for each col value, you can just use the below tbody code.
<tbody>
{% for row in rows %}
<tr>
{% for col in row %}
<td> {{ col }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
Hope it helps!

how to get value of download button into my django view?

I have a download button in my HTML that has a href to a path on my system for the corresponding file. How can I load that path into my view when at user clicks download? Also this value is unique for each download button.
If there's any other way that I can do this without exposing my system path in the href I would much prefer to know that. Thanks in advance.
Right now I have some HTML that looks like this. How do I grab the info from item.OutputPath into my view when clicked?
<div class="dashboard-2">
<div class="tasks-finished">
<h1>Finished tasks</h1>
</div>
<div class="tasks-list">
<table>
<tr>
<th>Name</th>
<th>Task ID</th>
<th>Status</th>
</tr>
{% for item in query_finished %}
<tr>
<td>{{ item.TaskNavn }}</td>
<td>{{ item.TaskID }}</td>
<td>Download </tr>
{% endfor %}
</table>
</div>
</div>
Additonal info:
I need this value because i'm trying to save it as a variable to serve protected files using Nginx.
Exposing the system path is a bad idea in itself, but using it as an input parameter would be a huge security risk.
It is better to pass the id of your item to your download view. Something like this:
# template
<td>Download</tr>
# urls.py
path('download/<int:pk>/', views.download_item, name='item-download'),
# views.py
def download_item(request, pk):
# Make sure to perform any required checks, e.g. item.owner=request.user
item = get_object_or_404(Item, pk=pk)
output_path = item.OutputPath
...

For Loop from database using Jinja2 Template

Goal: {% for loop %} over a list (using Jinja2) and then print out results {{print}} in a HTML table using Bootstrap.
Problem: List is not printing in the template.
In the view_config, I used query .all() to return a list of all the assessment_results objects. They are returning... I confirmed this via terminal/print debugging. However, the for loop is not returning the values needed to populate a table; as read in Jinja2 tutorial. I don't think I need to use a for loop in the view_config as I have seen others do (see here), but I am new to this and am trying to figure out how these two programs (SQLALCHEMY and Jinja2) interact.
An example from the printout after using .all() mentioned above:
[<Assessment_Result(owner='<User(username ='baseball', firstname ='Jen', lastname ='See', email='girl#aol.com')>', assessment='<Assessment(name='Becoming a Leader', text='better decisions')>')>]
view_config code:
views.py
#view_config(route_name='assessment_results', request_method='GET', renderer='templates/assessment_results.jinja2')
def all_assessment_results(request):
with transaction.manager: # < --- THIS WAS THE ISSUE !
assessment_results = api.retrieve_assessment_results()
if not assessment_results:
raise HTTPNotFound()
return {'assessment_results': assessment_results}
Corresponding Jinja2 template using Bootstrap:
assessment_results.jinja2
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<td> Assessment ID </td>
<td> Assessment </td>
<td> Owner </td>
</tr>
</thead>
<tbody>
<tr>
{% for x in assessment_results %}
<td>{{ x.assessments|e }}</td>
<td>{{ x.owners|e}}</td>
{% else %}
<td><em>no users found</em></td>
{% endfor %}
</tr>
</tbody>
</table>
</div>
You should look at the documentation
http://jinja.pocoo.org/docs/dev/templates/#for
You want to iterate over a dict, so consider using iteritems, itervalues or what ever you want.
Also note that your query will not return a dict, it will return a list or rows that matched.
I am also not sure if the for-else works in jinja. But you should avoid using that anyways.

Categories