I was trying to put a link in some text through url_for (flask, python3) and also pass a variable but nothing I tried worked .Here's the code I used. Am I doing something wrong?
html5:
<a class="mr-2" href={{ url_for('user','username'=posts.author)}} >TEXT</a>
python3:
#app.route('/user')
def user(username):
print(username)
You'd need to change the template code to look like:
<a class="mr-2" href="{{ url_for('user',username=posts.author) }}" >TEXT</a>
However you'd also have to change the python code for this to be valid:
#app.route('/user/<username>')
def user(username):
print(username)
A valid request is now:
/user/CodEdo
Which is the URL which should be rendered in the href assuming that posts.author in the template is CodEdo.
Related
I've been trying to generate URL's on a detailpage for my app. I want the url of the QR-Code to be a link to the detail page of the item. What i have right now is this:
<img src="{% qr_url_from_text "localhost:8000/items/{{item.id}}" %}" alt="QR-Code for the item">
the Problem with this is, that i want to give item.id as part of the url. But it's not seen as variable, so when i scan the qr-code, it opens the page http://localhost:8000/items/{{item.id}}, but i want it to have the actual id (e.g. 4) in the url, not {{item.id}} as string.
Is there any way to put variables in those URL names?
Thank you already
Do not make url using variable in template.
Do it in view, as context variable, and than use that variable in place of "localhost:8000/items/{{item.id}}" in template .
So, in your view, you will have something like:
def yourview(request, pk):
qrcode_url = "localhost:8000/items/" + str(pk)
context = {
qrcode_url: 'qrcode_url',
}
return render(request, 'yourtemplate.html', context)
and than in template:
<img src='{% qr_url_from_text qrcode_url size="t" version=10 image_format="png" error_correction="L" %}' alt="QR code">
I have a issue during using url_for in jinja template. I use like :
href="{{ url_for('user',user_id=current_user.id)}}"
when I write href="user/{{current_user.id}}" it works perfectly ::
and my view function is :
#app.route('/user/<user_id>')
def get_user(user_id):
user = Users.query.filter_by(id = user_id).first()
return render_template("profile.html", user = user)
but my jinja cant see this url and has an errors
werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'user' with values ['user_id']. Did you mean 'get_user' instead
As the error message suggested, you have to provide the name of the view function to url_for():
href="{{ url_for('get_user', user_id=current_user.id)}}"
I have a django template where i send an image actually its for an email.It only works when i keep the whole url like abc.com/images/a.png .
i have sent he image like this:
{
'picture':picture.url
}
This is the code for template that i am using for email template:
<img src="{{picture}}" alt="image" width="200" height="200" >
Am i missing something? as it only works when i keep the whole url?
To build an absolute URL you can use request.build_absolute_uri(picture.url) in your view.
This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 1 year ago.
I am trying to learn flask.
My login.html file-
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<table>
<tr><td>Name</td>
<td><input type ="text" name ="uname"></td></tr>
<tr><td>Password</td>
<td><input type ="password" name ="pass"></td></tr>
<tr><td><input type = "submit"></td></tr>
</table>
</form>
</body>
</html>
And my main.py file has this-
#app.route('/login',methods = ['POST'])
def login():
uname=request.form['uname']
passwrd=request.form['pass']
if uname=="ayush" and passwrd=="google":
return "Welcome %s" %uname
I am not able to understand how is this able to access login.html without specifying. Also also please explain what is the code in main.py means.
You have to specify the 'html' in flask to access it, however, if you open the html file in browser this will still work since its action is aimed directly at your flask server.
the code of your main.py says that if the in the form sent the data 'uname' and 'pass' are respectively 'ayush' and 'google', the code sends back to the browser a text indicating: "Welcome ayush"
If you want to directly implement the html in your flask web server, you have to create the function and put your html code in templates folder.
from flask import render_template
...
#app.route('/', methods=['GET'])
def code():
return render_template('index.html', name='')
So you can access with http://localhost:5000/ now
in views.py I have this:
def logout(request,key):
auth.logout(request)
return HttpResponseRedirect(reverse('airAgency.views.index',args=[key]))
in index template i wanna when user click on a link,logout view run:
logout
I wanna pass key parameter to logout view,Imagine I have an object named agn and it's WebSite field is going to pass to logout view, s.th like this:
<a href="{% url airAgency.views.logout agn.WebSite %}">
Imagine agn.WebSite has the value of mastane above code cause this error:
Caught NoReverseMatch while rendering: Reverse for 'airAgency.views.logout' with arguments '(u'mastane',)' and keyword arguments '{}' not found.
what's the right way to do this?
Caught NoReverseMatch - usually means that the url is being called by incorrect arguments, or that something else is wrong when {% url %} tries to call the url specified.
You need to define url pattern for that view function to be called in urls.py. And in the url pattern specify a view function.
urls.py
url(r'^logout/(?P<key>[a-zA-Z0-9\-]+)/$', "airAgency.views.logout", name="logout"),
index.html
<a href="{% url logout agn.WebSite %}">
This might not work if you just copy paste it because I don't know the exact project setup.
The key is to have urls.py where regex is used to create patterns for how the url looks like, then a view function that needs to be invoked, and finally a name, which you can then use in your templates.
This is all explained in the basic Django tutorial.