How to use links to local server as href in Django? - django

I want to have links that are clickable . That link is a path in a server.
Example:
file:///I:/IT/Install/Winrar/
SO far I have my view code:
def installables_available(request):
install_path = r'I:/IT/Install/'
list_setup = os.listdir(install_path)
full_path = [os.path.join(install_path, item) for item in list_setup]
return render(request, 'generic/installables.html', {'full_path': full_path})
And the html looks like:
<html lang="en">
<head>
<title>All Installables</title>
</head>
<body>
<h1>Below are all setups available</h1>
{% for name in full_path %}
<ul>
<li><a href={{name}}> {{name}}</a></li>
</ul>
{% endfor %}
<hr>
<p>Thanks for visiting my site.</p>
</body>
</html>
I do get the page showing links but , If I click them, they don't do anything. But hovering on them shows the correct path i.e e.g file:///I:/IT/Install/Winrar/.

Related

Add Task doesnot working in django in ToDo List

this is task_list.html
<!--if we didnt create this tasklis.html we get error becoz as said in views .py it looks for tempate
we diidnt cdreated any list on template et-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do List By Manasi</title>
</head>
<body>
{% if request.user.is_authenticated %}
<p>{{ request.user }}</p>
Logout
{% else %}
Login
{% endif %}
<hr>
<h2>WELCOME To My To List! </h2>
Add task
<table bgcolor="yellow" align="center" border="2px">
<tr>
<th>Task Name</th><th> Task Details</th><th>Edit Task</th><th align="center">Delete Task</th>
</tr>
{% for task in tasks %} <!--for task in object_list %}this is when we havent created objet context list yet in view.pywhen view.py only contains model=Task-->
<tr><td align="center">{{task.title}}</td><td align="center">View</td><td align="center">Edit</td>
<td align="center">Delete Task</td></tr> <!--i think this 'task' in url is context_objct_name in views.py but its false-->
{% empty %} <!--this is django template format for empty condition likewise ele etc.-->
<tr><td>No item</td></tr>
{% endfor %}
</table>
</body>
</html>
**this is task_form.html for add task ie.create task**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create task Form</title>
</head>
<body>
<h1>Create task form</h1>
Go Back
<form method="POST" action="">
{% csrf_token %} {{form.as_p}}
<input type="submit" name="Submit">
</form>
<!--django creates form for user model automatically on the basis of model attributes we provides while creating a form-->
<!--you wiill see the boxes an all is like a admin panel have but here is in horizotal manner so put as_p for vertical-->
</body>
</html>
I am creating a project "TO Do App" using django in pycharm.I created model for task first then create a taskview and detail vview and still here things are woked.then i created Create view and template for add task and things were worked. then created deleteview things are ok even i created login view. and then i created logout view,User registration.as i create user registration sucessfully i tried by registeriing for different users and creating their own tasklist but now add task is not working.
able to click on add task and afte filling details and presiing submit button i couldnt able to see the tasks in tasklist.
i can see these added tasks in admin panel but i cant see tasklist in list view template after creating logout and login and user registration.
also i couldnt see any errror after submiting task.
here is task_form.html page to after pressing add task in task_list.html it redirects to task_form.html
views.py screenshot 1:
viwes.py screenshot 2:
if your all the views like registration , login are working perfectly then there could be an error in the views where you render your task_list.html
can you share your view which renders your task_list.html and model of the to_do list

flask include template with variable

Just curious why this is working, since haven't found such examples in flask and jinjia2 official docs. It seems include is just simply put the content to the place just as it is, right?
# app.py
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
names = ['John', 'James', 'Jenny']
return render_template('index.html', names=names)
if __name__ == '__main__':
app.run(port=5200)
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
Welcome!
<!-- I know this is working as official docs -->
{% for name in names %}
<div class='card'>Welcome {{ name }}!</div>
{% endfor %}
<!-- but haven't found any examples like below, but it works -->
{% for name in names %}
{% include "card.html" %}
{% endfor %}
</body>
</html>
<!-- card.html -->
<div class='card'>Welcome {{ name }}!</div>
code example in vscode
From https://jinja.palletsprojects.com/en/3.0.x/templates/
Include
The include tag is useful to include a template and return the
rendered contents of that file into the current namespace:
{% include 'header.html' %}
Body {% include 'footer.html' %}
Included templates have access to the variables of the active context
by default.
This code will work because cards.html will be reloaded at each step of the "for" loop.
We can include a template in another template in the jinja structure. We don't have to use "for" when doing this.
The list elements to be navigated in the "for" loop are rendered from the flask view. In this way, we can use the rendered list and its elements as we want in the specified template while rendering.
{% for name in names %}
{% include "card.html" %}
{% endfor %}

Django - name 'title' is not defined

An hour ago I started to make a web application, with Django. I watched this video https://www.youtube.com/watch?v=qDwdMDQ8oX4
I'm following all his steps, but after changing some stuff, I've got an error which says
title is not defined in /about/.
Here's my code for the route /about/. The code is equal to my other home page, but this one doesn't work.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
{% if title %}
<title>Django BLog - {{ title }}</title>
{% else %}
<title>Django blog</title>
{% endif %}
</head>
<body>
</body>
</html>
As shown in the traceback, title is not defined in your view. This happens because title is not wrapped in quotes so it assumes it's a variable.
Add the quotation marks like so 'title' and you should be fine.
So, you should change line 29 in views.py to
return render(request, 'blog/about.html', {'title': 'About'})

using Flask Mail to send the rendered form

I am using flask_mail to send emails, while this works for text message, I need some help if i need to send a template itself (what does this mean: so I am rendering a report that contains multiple tables and a text area on the top with a submit button, once the user fills the text area and click on submit, I need flask to send the report containing table along with text data ).
This code fetches data from service now incident table
def incident():
service_now_url = SERVICE_NOW_URL
request_str = req
user = 'username'
pwd = 'password'
url = service_now_url + request_str
headers = {"Accept": "application/json"}
response = requests.get(url, auth=(user, pwd), headers=headers)
json_str = response.json()
records = json_str['result']
return records
This code below is used for rendering the text field and the table.
#app.route('/submit', methods=['GET','POST'])
def submit():
rec = incident()
form = Description(request.form)
shift = form.Shift.data
return render_template('index.html', rec=rec, state=STATES, form=form)
So for sending the email so far, I have written the function below, this sends an email but with table header only and no data.
def send_email(shift):
msg = Message(subject=shift ,recipients=['user#gmail.com'])
msg.html=render_template('index.html')
mail.send(msg)
I am not a Flask expert and still, in the learning phase, any help would be greatly appreciated.
#George thanks for your help, but this is not working, I have pasted below the html template with the modification suggested by you.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> {{ title }} : Graph</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style/main.css') }}">
{% if sending_mail %}
<style>
{{ get_resource_as_string('static\style\main.css') }}
</style>
{% endif %}
</head>
<body>
<form action="/submit">
<p> {{ shiftsum }}</p>
<div align='left'>
<h1>Task</h1>
<table id="customers">
<th>Task Number</th>
<th>Description</th>
<th>State</th>
{% for records in rec %}
<tr>
<td>{{records['number'] }}</td>
<td>{{records['short_description']}}</td>
<td>{{ state[records['state']]}}</td>
</tr>
{% endfor %}
</table>
</div>
</form>
</body>
</html>
Change the definition of send_email() to as given below,
def send_email(shift, rec, STATES):
msg = Message(subject=shift ,recipients=['user#gmail.com'])
msg.html=render_template('index.html', rec=rec, state=STATES)
mail.send(msg)
And in the index.html make sure you enclose the form inside {% if form %} ... {% endif %} where ... indicates your form code.
I hope this helps.
Update (for fixing the missing css styles)
Add the following in your flask script below app = Flask(__name__) or in respective blueprint file,
def get_resource_as_string(name, charset='utf-8'):
with app.open_resource(name) as f:
return f.read().decode(charset)
app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string
Add the following in the index.html
{% if sending_mail %}
<style>
{{ get_resource_as_string('static/css/styles.css') }}
</style>
{% endif %}
Where static/css/styles.css should be replaced with path to your css file. If more than one css file is there, just add {{ get_resource_as_string('static/css/styles.css') }} for each one of them, with their respective path as argument of get_resource_as_string()
Make the following changes in send_email(),
def send_email(shift, rec, STATES):
msg = Message(subject=shift ,recipients=['user#gmail.com'])
msg.html=render_template('index.html', rec=rec, state=STATES, sending_mail=True)
mail.send(msg)
I have added sending_mail=True as argument to the render_template() so whenever sending_mail is set, the render_template will add the content from the css files to the <style>...</style>.
I hope this fixes the missing css styles.
#George thanks for your help, anyways I have found out what was the missing link here,
so the thing is most of the email client doesn't support CSS that are stored locally(this what I found out, could be other things as well), so I used inline CSS and that is working perfectly and now I can see all the formatting that has been done inline in the HTML Template while sending the email.
thanks again for your help.

Django render_to_response displaying raw text

I am new to Django and I'm trying to create a simple html skeleton to verify everything is working properly. Everything is working (server is running and it loads the file) yet when I put in HTML code it is displayed as raw text instead of rendering it correctly.
My views.py is as follows
def home(request):
return render_to_response('index.html')
My 'index.html' is as follows
<!DOCTYPE html >
<html>
  <head>
   <meta charset="UTF-8">
   <title> awesome </title>
  </head>
  <body>
  
  </body>
</html>
What should I do to have it render correctly? (Display only "awesome")
EDIT
As far as this problem goes, the error came in that I saved the raw code as html. When I chose this option, it added the code to make html render it look like a raw input.
Moral of the story: Make sure you do your edits in a text editor and change extension by hand
A few problems..
1: what's with the spaces inside your tags?
< title > is invalid. It needs to be <title>Foo</title> That's why you're seeing "html".
2: Even if the title tag was written correctly, a title tag does not render, so you will get a blank page. If you want to display "awesome" -- you need to write it inside the body tag.
<body>awesome</body>
1) Remove spaces in < title > tag
2) And add bellow code in your urls.py file no need to map with view you can render html page from url also
(r'^home/$', 'django.views.generic.simple.direct_to_template',
{'template': 'index.html'}),
The first thing you need to do is create a "base" template so the other templates can extend from. You will normally call it base.html but you can use the name you want. You also need to create blocks that extended templates can use:
base.html
<!DOCTYPE html >
<html>
<head>
<meta charset="UTF-8">
< title > awesome < /title >
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Then, you have to extend base.html from your index.html and use the content block we have created:
index.html
{% extends "base.html" %}
{% block content %}
{% endblock %}
At this point, index.html will be exactly as base.html because you are not showing anything inside the content block. Update your view with some data like this:
views.py
def home(request):
data = {'name': 'YourName', 'age': 25}
return render_to_response('index.html', data)
Now, again, update your index.html:
index.html
{% extends "base.html"%}
{% block content %}
<p>My name is {{ name }}</p>
<p>I'm {{ age }} years old</p>
{% endblock %}
Don't forget to read the fine tutorial.