Im pretty new to django. I have created an website that outputs data from an API to an table in django.
I created an app called 'display_networks'. Inside the views.py of this app i have the following code:
from django.shortcuts import render
from django.views.generic import TemplateView
from .services import get_networks
# Create your views here.
class GetNetworks(TemplateView):
template_name = 'networks.html'
#context == dict
def get_context_data(self, *args, **kwargs):
context = {
'networks' : get_networks(),
}
return context
As you can see i import an function called 'get_networks' from .services (services.py).
Inside services.py i have the following code:
import os
import requests
import json
# Headers
apikey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
headers = {'x-cisco-meraki-api-key': format(str(apikey)), 'Content-Type': 'application/json'}
# Headers
def get_networks():
url = 'https://api.meraki.com/api/v0/organizations/XXXXX/networks/'
r = requests.get(url,headers=headers)
networks = r.json()
return networks
Inside my app i have created a templates folder with an index.html. Inside this index.html i have the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Meraki Networks</title>
<link crossorigin="anonymous"
href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css"
integrity="sha256-8B1OaG0zT7uYA572S2xOxWACq9NXYPQ+U5kHPV1bJN4="
rel="stylesheet"/>
<link rel="shortcut icon" type="image/png"/>
</head>
<body>
<nav aria-label="main navigation" class="navbar is-light" role="navigation">
<div class="navbar-brand">
<div class="navbar-item">
<img alt="Meraki" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Meraki_Logo_2016.svg/1920px-Meraki_Logo_2016.svg.png"
style="margin-right: 0.5em;" width="142" height="142">Networks
</div>
</div>
</nav>
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
<thead>
<tr>
<th>Network Name</th>
<th>Network ID</th>
<th>Time zone</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
{% for network in networks %}
<tr>
<td>{{ network.name }}</td>
<td>{{ network.id }}</td>
<td>{{ network.timeZone }}</td>
<td>{{ network.tags }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
This works good and i have the data i want to display.
What i want to do is add an extra column to my table with the header "Get device info" and with an button titled 'Get devices' as the table data.
What this button has to do is send the variable 'network.id' to an different function in my services.py.
I have created a new function inside my services.py with the following code:
def get_devices(net_id):
url = 'https://api.meraki.com/api/v0/networks/'+net_id+'/devices/'
r = requests.get(url,headers=headers)
devices = r.json()
return devices
The variable "net_id" has to come from the button click and the button has to call this function and display the output the same as i have done for the networks.
Does anybody have an idea how i can achieve this?
Thanks in advance!
As the question asks how to pass a variable to a view. The answer is:
Pass it inside the url.
Pass it as a GET or POST parameter in the request (can be done using forms etc.).
Pass it inside the request body.
To solve your problem we shall use the 1st option for it's simplicity. First in your urlpatterns we shall make a pattern that will capture some arguments and these shall be passed to the view:
urlpatterns = [
# Other patterns
path('device/<str:network_id>/', views.GetDevices.as_view(), name='get_devices'),
# Other patterns
]
The <str:network_id> here means that network_id would be passed as a keyword argument to the view. Check more details about the URL dispatcher.
Your view GetDevices would handle getting the devices:
from .services import get_devices
class GetDevices(TemplateView):
template_name = 'devices.html'
#context == dict
def get_context_data(self, *args, **kwargs):
context = {
'devices' : get_devices(kwargs['network_id']),
}
return context
Now in your template with the table for networks you would write the following to add an anchor tag with a link to this new page:
<td>Get Devices</td>
You can style this anchor to look like a button using css / actually put a button tag inside it.
I am sure you know how to make templates so I would leave the task of making 'devices.html' to you.
Related
I am attempting to create a view that allows users to delete a build log. On the view that shows the delete button with a link to the delete page I am getting the error
Reverse for 'build-log-delete' with no arguments not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/build\\-log/(?P<pkz>[0-9]+)/delete$']
If I understand this error correctly its because I am not passing paramaters in the url.
<a class="delete-btn" href="{% url 'build-log-delete' %}">Delete</a>
However I do not understand why I need to pass parameters in the URL as I am not passing any new values into the URL and if so what parameters I would pass. Do I have to re pass the previous two?
urls
path('post/<int:pk>/build-log/<int:pkz>/', views.BuildLogDisplay, name='build-log-view'),
path('post/<int:pk>/build-log/<int:pkz>/delete', views.BuildLogDelete, name='build-log-delete') #error
views
def BuildLogDisplay(request, pk, pkz ):
post = BuildLog.objects.filter(post_id=pk)
log = BuildLog.objects.get(pk=pkz)
context = {
'post':post, 'log':log
}
return render(request, 'blog/buildlog.html', context)
def BuildLogDelete(request):
context = { }
return render(request, 'blog/BuildLogDelete.html', context)
full template
<div class="row">
<article class="cars-article">
<div class="flex">
<img class="rounded-circle article-img" src="{{ log.author.profile.image.url }}" />
<div>
<a class="article-title">{{ log.title }}</a>
</div>
</div>
<br>
<div>
{% if log.author == user %}
<a class="update-btn" href=""> Update</a>
<a class="delete-btn" href="{% url 'build-log-delete' %}">Delete</a>
{% endif %}
</div>
<hr class="solid">
<p class="article-content">{{ log.content | safe}}</p>
</article>
</div>
There are multiple errors in you code. You are not passing args in BuildLogDelete view but in url you are using those arguments. So the correct view should look like this.
def BuildLogDelete(request,pk,pkz):
# delete code
# write here
Next mistake which i can see is you are assigning queryset rather than object for the post key in BuildLogDisplay view. You should assign object.
post = BuildLog.objects.get(post_id=pk)
Lastly your original error mentioned in the question is because your build-log-delete url expects two arguments i.e pk and pkz but you haven't passed them in template. So it should be like this.
<a class="delete-btn" href='{% url "build-log-delete" pk=post.post_id pkz=log.pk %}'>Delete</a>
I would highly suggest you to look for already given generic views like ListView, TemplateView, CreateView, UpdateView and DeleteView. This will prevent you from reinventing the wheel
Ref: Django Class Based Generic Views
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
I am trying to export django models data (admin side data) into a PDF file.
For that first I created a HTML file to render the data from models.
The HTML file I created
It worked successfully and showed the data from the models correctly.
Successfully worked (I create a url for it to check whether it is working or not)
Then I tried to render the same html file to PDF. I ran the server and I generated a pdf file.
PDF file I expected it will show the data also. But It only showed the table border.
You can see my folders and names in the 1st photo.
I thing it is enough to add this code. If you need full code please tell me.
This is my views.py from app.
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
class ViewPDF(View):
def get(self, request, *args, **kwargs):
pdf = render_to_pdf('app/pdf_template.html')
return HttpResponse(pdf, content_type='application/pdf')
Can't I use the same html file to get the data as pdf?
Can anyone tell me what wrong I did?
pdf_template.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>This is my first pdf</title>
</head>
<body>
<center>
<h2>User Table</h2>
<table border="1">
<tr>
<th>Username</th>
<th>E-mail</th>
<th>Country</th>
<th>City</th>
</tr>
{% for result in user %}
<tr>
<td>
{{result.username}}
</td>
<td>
{{result.email}}
</td>
<td>
{{result.country}}
</td>
<td>
{{result.city}}
</td>
</tr>
{% endfor %}
</table>
</center>
</body>
</html>
I got the correct out put. I am posting this as an answer so that it will use for someone else too.
class ViewPDF(View):
def get(self, request, *args, **kwargs):
context={}
context['user'] =user.objects.all()
pdf = render_to_pdf('app/pdf_template.html',context_dict=context)
return HttpResponse(pdf, content_type='application/pdf')
In the views.py I modified the code like above.
i see you used 'user' as a list for your 'for' condition but you never add it to your context. i think it's working fine but there is no data to show
update:
in the 'render_to_pdf' you get 'context_dict' argument to render your template by that. but you never pass this argument when you call your function. that's why you can' see anything except borders. because there is no data
update 2: in this line :
pdf = render_to_pdf('app/pdf_template.html')
just add context dict. something like this:
pdf = render_to_pdf('app/pdf_template.html',context)
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.
I have read this post: How can I create a text link in a Knockout javascript table? along with a couple others.
But, I am missing something somewhere, or not taking the right approach. I've included the relevant chunks of code for my problem. I am trying to use the table generated by knockout to to either update a task or remove a task. The remove part is working fine. I am trying to get the update to link to another page that is used to update the task. I cannot figure out what I need to do to get the link working properly in the update column.
I've tried several different approaches for how to put the url in the list of dictionaries that is passed to the KO model. Any advice to steer me in the right direction? If I am missing any information, please let me know. Thank you.
Views.py
def TaskList(request, job_id):
job_tasks = Tasks.objects.filter(parent=job_id)
tasks_list = []
for task in job_tasks:
task_row = {}
task_row['task_id'] = task.task_id
task_row['t_name'] = task.name
task_row['date'] = task.date_created
task_row['state'] = task.state
task_row['url'] = '{% url tracking:update_task task_id=task.task_id %}'
tasks_list.append(task_row)
json_tasks = json.dumps(tasks_list)
if request.POST:
json_data = request.POST.get('json_blob')
obj = loads(json_data)
task.task_id = obj.get("task_id")
remove_task = Tasks.objects.get(task_id=task.task_id)
remove_task.delete()
messages.success(request, 'Task removed')
HTML
<table>
<thead>
<th>Name</th>
<th>Date</th>
<th>State</th>
<th>Update</th>
<th>Remove</th>
</thead>
<tbody data-bind "foreach: tasks">
<tr>
<td data-bind="text: t_name"></td>
<td data-bind="text: date"></td>
<td data-bind="text: state"></td>
<td a class="btn" data-bind="attr: {href: url}">Update</a></td>
<td button class="btn" data-bind="click: $root.remove_task">Remove</button></td>
</tr>
</tbody>
</table>
{% block javascript_variables_nocompress %}
window.TASKS = {{ json_tasks|safe }};
{% endblock %}
{% block javascript_compress %}
<script type='text/javascript' src="{% static 'js/knockout/knockout.js' %}"></script>
<script type="text/javascript">
$(function() {
var RemoveTaskModel = function () {
var self = this;
self.tasks = ko.observableArray(window.TASKS);
self.remove_task = function(task) {
self.tasks.remove(task);
$("#json_blob").val(ko.toJSON(task));
}
}
ko.applyBinding(new RemoveTaskModel());
});
</script>
{% endblock %}
HTML
I would use reverse to do a reverse lookup of the URL for each task:
from django.core.urlresolvers import reverse
def TaskList(request, job_id):
job_tasks = Tasks.objects.filter(parent=job_id)
tasks_list = []
for task in job_tasks:
...
task_row['url'] = reverse('update_task', args=(),
kwargs={'task_id': task_id})
Then your observableArray should be able to bind the property from the JSON to the anchor tag. You might also note that in your code sample, your td is malformed:
<td a class="btn" data-bind="attr: {href: url}">Update</a></td>
it should be:
<td><a class="btn" data-bind="attr: {href: url}">Update</a></td>