I'm 100% sure my path is correct in my code, why is it giving me TemplateDoesNotExist error? I can't see how this is possible.
I tried:
settings.py > TEMPLATES > Dirs: [/template/music]
and a laundry list of other things on SO but it doesn't rectify my problem.
Here's my views.py file:
from django.http import HttpResponse
from django.template import loader
def index(request):
template = loader.get_template('music/index.html')
return HttpResponse(template.render())
def detail(request, user_id): # Testing out page 2
return HttpResponse("<h2>Page # (testing this out) " + str(user_id) + "</h2>")
Here's my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="#">
<div class="form-group">
<label for="firstName">First Name:</label>
<input type="email" class="form-control" id="firstName" placeholder="Enter first name" name="firstName">
</div>
<div class="form-group">
<label for="">Last Name:</label>
<input type="email" class="form-control" id="lastName" placeholder="Enter last name" name="lastName">
</div>
</form>
<div class="checkbox">
<label><input type="checkbox" name="remember">Remember me</label></div></br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</body>
</html>
Your templates directory is inside migrations. Move it out of there.
Related
This question already has answers here:
How to serve static files in Flask
(24 answers)
Closed 11 months ago.
I created a Flask app to upload an image to predict its label. This is the code of the relevant part of app.py:
#app.route("/", methods=['GET', 'POST'])
def main():
return render_template("index.html")
#app.route("/submit", methods = ['GET', 'POST'])
def get_output():
if request.method == 'POST':
img = request.files['my_image']
licenseplate = "static/" + img.filename
img.save(licenseplate)
p = predictLicensePlate(licenseplate)
This is the index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Number Plate Recognition</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1 class="jumbotron bg-primary">Number Plate Recognition</h1>
<br><br>
<form class="form-horizontal" action="/submit" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Upload Your Image :</label>
<div class="col-sm-10">
<input type="file" class="form-control" placeholder="Hours Studied" name="my_image" id="pwd">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
{% if prediction %}
<img src="{{img_path}}" height="200px" width="300px">
<h2> Your Prediction : <i> {{prediction}} </i></h2>
{% endif %}
</div>
</body>
</html>
But when I upload an image I only get a placeholder image there as you see below. Can you figure out what is wrong with my code? The format of my image is png and the prediction works fine.
You need to set the img_path variable in your template:
<img src="{{img_path}}" height="200px" width="300px">
i am new to django/python
i am trying to change user password but i am getting error
Method Not Allowed (POST): /password_change_done
Method Not Allowed: /password_change_done
[23/Jul/2020 19:11:05] "POST /password_change_done HTTP/1.1" 405 0
this is my url patterns just for password changes :
path('password_change',
auth_views.PasswordChangeView.as_view(template_name='password_change.html'),
name='password_change'),
path('password_change_done',
auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'),
name='password_change_done'),
both my html pages for this operation
my html code: password_change
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome</title>
</head>
<body>
<h3> Change Password</h3>
<form action="{% url 'password_change_done' %}" method="POST" class="form-signin">{% csrf_token %}
<input name="Old password" class="form_control" placeholder="Old password"
type="password" id=old_password required="true">
<input name="new password" class="form_control" placeholder="new password"
type="password" id=new_password required="true">
<input name="confirm password" class="form_control" placeholder="confirm password"
type="password" id=confirm_password required="true">
<button type="submit">Update</button>
</form>
</body>
</html>
my html code for password_chang_done:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome</title>
</head>
<body>
<h1>Welcome</h1>
<h2> <img src="{% static 'images/welcome.jpg' %}"> </h2>
<h5>hello,{{request.user.username}}</h5>
<script>
window.alert("password sucessfully changed")
</script>
</body>
</html>
You should make the POST request to the password_change endpoint, not the password_change_done` endpoint, so:
<form action="{% url 'password_change' %}" method="POST" class="form-signin">
…
</form>
In is quite common in Django web development, and in web development in general to make a POST to the same view that first generated the page with a GET request.
Furthermore the PasswordChangeView view uses the PasswordChangeForm [GitHub]:
class PasswordChangeForm(SetPasswordForm):
# …
field_order = ['old_password', 'new_password1', 'new_password2']
The name of the <input> elements thus should be old_password, new_password1, and new_password2:
<input name="old_password" class="form_control" placeholder="Old password" type="password" id=old_password required="true">
<input name="new_password1" class="form_control" placeholder="new password" type="password" id="new_password" required="true">
<input name="new_password2" class="form_control" placeholder="confirm password" type="password" id=confirm_password required="true">
The goal of my program is to place the first and last name into my database upon it being entered and Submit button clicked by the user so that when I manually check in Terminal what’s in my database, I can see what the user has entered and submitted. I need to some how connect my Submit button to my views.py file so that it can go through , sort like an onClick() but this time, to go a .py file. (correct me if I’m wrong with this train of thought).
How would I go about making this happen?
Here's my views.py file:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Person
def index(request):
if request.method == 'POST':
first_name = request.POST.get('firstName')
last_name = request.POST.get('lastName')
if first_name and last_name:
user = Person.objects.create(firstName=first_name, lastName=last_name)
user.save()
return render(request, 'music/index.html')
def detail(request, user_id): # Testing out page 2
return HttpResponse("<h2>Page # (testing this out) " + str(user_id) + "</h2>")
Here's my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="index.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="#">
<div class="form-group">
<label for="firstName">First Name:</label>
<input type="email" class="form-control" id="firstName" placeholder="Enter first name" name="firstName">
</div>
<div class="form-group">
<label for="">Last Name:</label>
<input type="email" class="form-control" id="lastName" placeholder="Enter last name" name="lastName">
</div>
</form>
<div class="checkbox">
<label><input type="checkbox" name="remember">Remember me</label></div></br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</body>
</html>
your form should be declared as:
<form method="POST">
and your <button type="submit"> should be inside the <form></form>.
As the same view deals with the GET and POST methods, you should remove the action="#" attribute, that way action will point to the same view.
I am trying to build a blog using flask. SimpleMDE is used as post editor(html code below). I want to save the markdown content to local file and render by flask-misaka in jinja2.
In SimpleMDE, I can get raw markdown content by simplemde.value(). But when I pass simplemde.value() to var in javascript. "\n" is missing aftering passing. I think it may have some "magic" tools in javascript. The html code return 2 alert message, first message contains line feed, the second doesn't.
Could somebody give me some hits about this problems?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Editor</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel='stylesheet' href='.{{ url_for("static", filename="css/simplemde.min.css") }}'>
<script src='.{{ url_for("static", filename="js/simplemde.min.js") }}'></script>
</head>
<script type='text/javascript'>
function check() {
var raw = simplemde.value();
alert(raw);
document.testform.markdown_raw.value=raw;
alert(document.testform.markdown_raw.value);
}
</script>
<body>
<form method='post' class='form' role='form' name='testform'>
<div class="form-group " style="padding-top:10px">
<input class="form-control" id="post_title" name="post_title" type="text" value="Title?">
</div>
<div class="form-group">
<input class="form-control" id="markdown" name="post_content" type="textarea" value="">
</div>
<div class="form-group" style='display:none'>
<input class="form-control" id="markdown_raw" name="markdown_raw" type="textarea" value="Origin">
</div>
<div>
<input class='btn btn-default' onclick="check();" id='submit' name='submit' type='submit' value='Save'>
<input class='btn btn-default' id='publish' name='publish' type='submit' value='Publish'>
</div>
</form>
<script type='text/javascript'>
var simplemde = new SimpleMDE({ element: document.getElementById('markdown') });
</script>
</body>
</html>
If you want to get raw markdown, just use simplemde.value().
When you put raw markdown into normal textarea, it turns into pure text.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Editor</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
</head>
<body>
<form method='post' class='form' role='form' name='testform'>
<div class="form-group " style="padding-top:10px">
<input class="form-control" id="post_title" name="post_title" type="text" value="Title?">
</div>
<div class="form-group">
<textarea class="form-control" id="markdown" name="post_content"></textarea>
</div>
<div>
<input class='btn btn-default' onclick="" id='submit' name='submit' type='submit' value='Save'>
<input class='btn btn-default' id='publish' name='publish' type='submit' value='Publish'>
</div>
</form>
<script type='text/javascript'>
var simplemde = new SimpleMDE({ element: document.getElementById('markdown') });
</script>
</body>
</html>
Get the content:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def test():
if request.method == 'POST':
raw_md = request.form['post_content']
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
I am completely lost as to why my webpages are not linking. They don't appear to be referencing the correct file paths ... I have tried to link the following 3 pages.
The homepage
The 'artists' icon on the top right of the homepage with the 'registerprofessional.html' so that when I click on 'artists' it takes me to the registration page
The 'customer' icon on the top right of the homepage with the 'registeruser.html' so that when I click on 'customer' it takes me to the registration page
I have added my code below:
home.html
registerprofessional.html
registeruser.html
file structure attached as image
registerprofessional error I get when clicking on the links added as images
register user error I get when clicking on the links added as images
urls.py
Please let me know if I need to add anything else.
See code below:
1) home.html
<!doctype html>
<html>
<head>
<title>ArtistsDecoded</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/static/web/css/vendor/bootstrap.min.css">
<link rel="stylesheet" href="/static/web/css/home.css">
<!-- background image changer
<script type="text/javascript">
$(window).load(function() {
var i =0;
var images = ['images/homepage/Arianna.jpg','images/homepage/vintagecam.jpg','images/homepage/10505026_413590265448462_3108080538408848321_o.jpg'];
var image = $('#slideit');
//Initial Background image setup
image.css('background-image', 'url(images/homepage/Arianna.jpg)');
//Change image at regular intervals
setInterval(function(){
image.fadeOut(1000, function () {
image.css('background-image', 'url(' + images [i++] +')');
image.fadeIn(1000);
});
if(i == images.length)
i = 0;
}, 5000);
});
</script>
-->
</head>
<body style="background-image:url(/static/web/img/homepage/arianna.jpg)">
<!-- background image changer not working <div id="slideit" style="width:700px;height:391px;"></div>
-->
<div class="container">
<!--row 1-->
<header class="row">
<div class="col-xs-3">
<div id="title">
<p><i>ArtistsDecoded</i></p>
</div>
</div>
<div class="col-xs-5"></div>
<div id="loginbar">
<div class="col-xs-1">
<div id="createprofile">
<p>Artists</p>
</div>
</div>
<div class="col-xs-1">
<div id="signup">
<p>Customers</p>
</div>
</div>
<div class="col-xs-1">
<div id="help">
<p>Help</p>
</div>
</div>
<div class="col-xs-1">
<div id="login">
<p>Log In</p>
</div>
</div>
</div>
</header><!--top bar-->
<!--row 2-->
<div class="row">
<div class="col-md-12 center-block">
<div class="welcome_text_div center-block">
<br></br><br></br><br></br><br></br><br></br><br></br><br></br>
<div class="how_it_works_box text-center">
<a href="#" id="how_it_works">
How It Works
</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 center-block">
<div class="searchbardiv text-center">
<form action="">
<br></br><br></br><br></br><br></br>
<select class="selectprofession">
<option>What Are You Looking For?</option>
<option>Hair Stylist</option>
<option>Henna Artist</option>
<option>Make-up Artist</option>
<option>Photographer</option>
<option>Videographer</option>
</select>
<input id="locationbox" type="text" placeholder="Where?"/>
<input class="datebox" type="date" name="Date From?"/>
<input class="datebox" type="date" name="Date To?"/>
<input id="search" type="submit" value="Search" />
</form>
</div>
</div>
</div>
</div><!--container-->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="/static/web/js/bootstrap.min.js"></script>
</body>
</html>
2) registerprofessional.html
<!doctype html>
<html>
<head>
<title>ArtistsDecoded</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--bootstrap-->
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/registertemp.css">
</head>
<body>
<div class="row">
<div class="col-xs-12">
<div class="title"><i>ArtistsDecoded</i></div>
</div>
</div>
<hr>
<div class="container">
<br></br>
<div class="row">
<div class="col-xs-12">
<div class="form">
<h4><img class="registerimage" src="Images/Registertemp/registertemp.jpg" alt="register" height="35px"></img><strong>Register Your Serices</strong></h4>
<p>Be the first to know when we launch this site!<p>
<form role="form">
<div class="form-group">
<Select name="Profession" multiple>
<option>Select Profession*</option>
<option value="make-up artist">Make-up Artist</option>
<option value="mehndi artist">Mehndi Artist</option>
<option value="hair stylist">Hair Stylist</option>
<option value="photographer">Photographer</option>
<option value="dhol group">Dhol Group</option>
</Select>
</div>
<div class="form-group">
<label class="sr-only" for="firstname">First name:</label>
<input type="name" class="form-control" id="fname" placeholder="First name">
</div>
<div class="form-group">
<label class="sr-only" for="surname">Surname:</label>
<input type="name" class="form-control" id="sname" placeholder="Surname">
</div>
<div class="form-group">
<label class="sr-only" for="email">Email address:</label>
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<label class="sr-only" for="number">Phone number:</label>
<input type="text" class="form-control" id="phone" placeholder="Phone number">
</div>
<div class="form-group">
<label class="sr-only" for="instagramid">Instagram ID:</label>
<input type="text" class="form-control" id="instagram" placeholder="Instagram ID">
</div>
<div class="form-group">
<label class="sr-only" for="facebookid">Facebook ID:</label>
<input type="text" class="form-control" id="facebook" placeholder="Facebook ID">
</div>
<div class="form-group">
<label class="sr-only" for="twitterid">Twitter ID:</label>
<input type="text" class="form-control" id="twitter" placeholder="Twitter ID">
</div>
<p class="selectinstructions">*Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>
<div class="checkbox">
<label><input type="checkbox">I have read & agree with the terms and conditions</label>
</div>
<button type="submit" class="btn btn-primary btn-md" rel="contact_form1" id="contact_form1_button">Submit</button>
</form>
</div><!--form-->
</div><!--col-->
</div><!--row-->
</div><!--container-->
<script src="jquery/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
3) registeruser.html
<!doctype html>
<html>
<head>
<title>ArtistsDecoded</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--bootstrap-->
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/registertemp.css">
</head>
<body>
<div class="row">
<div class="col-xs-12">
<div class="title"><i>ArtistsDecoded</i></div>
</div>
</div>
<hr>
<div class="container">
<br></br>
<div class="row">
<div class="col-xs-12">
<div class="form">
<h4><img class="registerimage" src="Images/Registertemp/findservices.jpg" alt="register" height="35px"></img><strong>Looking for a Professional</strong></h4>
<p>Be the first to know when we launch this site!<p>
<form role="form">
<div class="form-group">
<label class="sr-only" for="firstname">First name:</label>
<input type="name" class="form-control" id="fname" placeholder="First name">
</div>
<div class="form-group">
<label class="sr-only" for="surname">Surname:</label>
<input type="name" class="form-control" id="sname" placeholder="Surname">
</div>
<div class="form-group">
<label class="sr-only" for="email">Email address:</label>
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<label class="sr-only" for="number">Phone number:</label>
<input type="text" class="form-control" id="phone" placeholder="Phone number">
</div>
<div class="checkbox">
<label><input type="checkbox">I have read & agree with the terms and conditions</label>
</div>
<button type="submit" class="btn btn-primary btn-md" rel="contact_form1" id="contact_form1_button">Submit</button>
</form>
</div><!--form-->
</div><!--col-->
</div><!--row-->
</div><!--container-->
<script src="jquery/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
5) Error - Registerprofessional
6) Error - Registeruser
7) urls.py
"""
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', 'src.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
]
Thanks in advance for any help
You have no URLs configured. Django can't handle a URL if you haven't told it how to handle them.
Go to the tutorial, and follow it.