Add Task doesnot working in django in ToDo List - django

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

Related

How to merge the header with main page in Django template where both are getting data from different querysets?

I have a situation where I want to create search in my page. I want to create search without it mixing with the original page as I need to use this same search in more than one page. I created search table in a div in templates folder and named it MySearch.html. Now, I have included that in the main page as {% include 'MySearch.html'%} and it is able to give me the drop down with static text but not with the options that I am filling with query set.
In urls.py -
url(r'Search', myproj.type4.views.ShowSearch, name='Search'),
In ShowSearch() -
def ShowSearch(request):
countryqueryset = Type4Main.objects.all().values('country').distinct()
return render(request,'MySearch.html',{
'countryqueryset':countryqueryset,
})
In MySearch.html -
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<head>
<meta charset="UTF-8"/>
<title> My Search
</title>
</head>
<body>
<div id ="mysearch" name="mysearch">
<table id="mysearchtbl" name="mysearchtbl">
<tr>
<th>
Country
</th>
</tr>
<tr>
<td>
<select id="country">
<option value="0">Select</option>
{% for country in countryqueryset %}
<option value="{{country.country}}">{{country.country}}</option>
{% endfor %}
</select>
</td>
</tr>
</table>
</div>
</body>
</html>
I can only see Select as option when it is merging with the main page. What am I doing wrong?
Views render templates, not the other way round: templates don't call views. If you are not viewing the page via the ShowSearch URL then the data from that view won't be passed to the template.
For data that needs to be included on every page regardless of the view, use a custom template tag.

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.

How to use links to local server as href in 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/.

Do I have t paste google analytics on evey page of my dgango app or just the base template?

Do I have to paste my ananlytics code into every page of my django app or can I just do it in one spot, the base, similar to disqus?
You can do it in the base page and that will be included in every actually generated page.
You can have, for example, a master_page.html in which you put the main wrapper HTML including your Google Analytics code. The main part of your master page would have:
<html>
<head>
<!-- Google Analytics code -->
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Then your content page will have something like:
{% extends "master_page.html" %}
{% block content %}
Your content for the page.
{% endblock %}

build website for mobile and pc with django

I am trying to develop a website for mobile and pc browser with django.
and I am trying to figure out a best structure of the views and templates.
there is what I have tried:
1) use different url ( like http://example.com/mobile/ and http://example.com/ OR
http://example.com/?c=mobile ) to distinguish mobile and pc, and map them to different view which set different templates.
2) in the view set different template according to USER_CLIENT
3) use a wrapper layer of the view, the actual view just return the data to the wrapper, the wrapper set the different template.
Is there a common way to handle this in django? any suggestions and comments?
Use Django's "sites" framework for a mobile version at http://m.example.com.
I would recommended solution 3; using a decorator to inspect the clients User Agent and returning à different template in case of a mobile agent.
Have the decorator take two arguments: the normal template, and the mobile template.
From your view, return a dict The decorator may pass to the rendering function as context. There is a decorator called 'render_to' that does this very well, Google for it.
To deal with the use case where users want the full version, even when browsing from a mobile device, you may use a redirecting view that sets a cookie your decorator may check for.
best practice: use minidetector to add the extra info to the request, then use django's built in request context to pass it to your templates like so.
from django.shortcuts import render_to_response
from django.template import RequestContext
def my_view_on_mobile_and_desktop(request)
.....
render_to_response('regular_template.html',
{'my vars to template':vars},
context_instance=RequestContext(request))
then in your template you are able to introduce stuff like:
<html>
<head>
{% block head %}
<title>blah</title>
{% if request.mobile %}
<link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-mobile.css">
{% else %}
<link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-desktop.css">
{% endif %}
</head>
<body>
<div id="navigation">
{% include "_navigation.html" %}
</div>
{% if not request.mobile %}
<div id="sidebar">
<p> sidebar content not fit for mobile </p>
</div>
{% endif %>
<div id="content">
<article>
{% if not request.mobile %}
<aside>
<p> aside content </p>
</aside>
{% endif %}
<p> article content </p>
</aricle>
</div>
</body>
</html>