I've got the following code within my html template:
Template
<div class="submitbutton">
<a href="{% url 'accounts:customerhomepage' %}" target="_blank" value="1">
<button>
View Demo
</button>
</a>
</div>
I'd like to pass the value of 1 to the function in views.py that will render the page. Within the view, I've got the following code:
Views.py
demo = request.GET.get('value')
print(demo)
The printed result is 'None' rather than '1'. I'm not sure where my code is wrong.
Thanks!
I think you need to have "value" in a query string like so:
<a href="{% url 'accounts:customerhomepage' %}?value=1" target="_blank">
Related
I'm trying to pass list values (from 'ci' list below) into my wepage using python. The list values contain URLs for images which will go in the HTML <img src {{ listname[1] }} tag (see HTML below for more info). However, when I try the below code and render the template on my local server the img does not appear and when I inspect element the img src tag is empty.
My code is below:
Python
#app.route('/route', methods=['GET', 'POST'])
def _get_gallery():
df=pd.read_csv('C:\\username\\foldername\\excelfile.csv')
images=list(df["image"].values)
clean_images=[]
for image in images:
if "https" in str(image):
clean_images.append(image)
ci=pd.DataFrame(clean_images)
return render_template('template.html', ci=ci)
if __name__ == '__main__':
app.run(debug=True)
The HTML template has the following code to try and pull through the list values:
<div class="row">
<div class="col">
<a href="{{ ci[1] }}">
<img class="img" src="{{ ci[1] }}" alt="">
</a>
</div>
I think you don't need to convert the list to pandas DataFrame, try to comment this line and see if its work
I have a table, where I add in the last column of every row the buttons “delete” and “edit”. I do this with the url + parameters in the href in the template (see below). I wrote a function for every href + parameter and the scripts work.
<form method="post">
{% csrf_token %}
<input type="hidden" name="projekt_id" value="{{objekt.id}}" />
<a class="btn btn-outline-secondary btn-sm" href="{% url 'check:remove_project' objekt.id %}" role="button">delete</a>
<a class="btn btn-outline-secondary btn-sm" href="{% url 'check:edit_project' objekt.id %}" role="button">edit</a>
</form>
Since i need such tables very often I want to handle the entire functionality (view the data/edit/delete/create) in one single view (I already have this in one template). My idea/wish is to pass the name= and value= from inside the buttons to the view. There I can distinguish for the appropriate functions - by if-statements- between edit/delete/view/create…
How can the parameters be passed from the BUTTONS in template to the view? Where is the documentation?
I wonder if there is a more elegant way to solve this? (maybe a combination of class based views?)
You can access a button like any other field in the POST data.
<button type="submit" name="delete">Delete</button>
<button type="submit" name="edit"> /Edit</button>
if "edit" in request POST:
...
elif "delete" in request.POST:
...
I am trying to set dynamic id attribute to the <body> tag in the HTML.
Something like this - <body id="{{ django_view_name }}>"
I want the id attribute to have the page name, like for the homepage id="home" and for the blog page id="blog" and contact page id="contact"
I don't want to use Javascript or Jquery.
I created a main.html template and then i am inheriting the main template in each of the other templates like index.html templates.
The code in main.htmltag looks like this -
<div class="content-wrapper" id="content">
<%include file="${static.get_template_path('header.html')}" args="online_help_token=online_help_token" />
${self.body()}
</div>
and then on the index.html template i am inheriting like this -
<%inherit file="main.html" />
UPDATE: REQUIRED ADDITIONAL INFORMATION
How can i evaluate the value of ${ request.resolver_match.url_name }. For example if id="${ request.resolver_match.url_name }" evaluates to id="home", then I want to do something like this -
%if ${ request.resolver_match.url_name }=root:
<div class="container">
else:
something_else
How can i do this? Any help is really appreciated.
Try this,
<body id="{{ request.resolver_match.url_name }}">
this will generate id based on your url name you defined in urls.py urls
Update:
After the edit of question i see that you used mako templates
So you need,
<body id="${ request.resolver_match.url_name }">
Basically I have a django template from which I show data about a document and I want a link to download the file. I tried something like this but it is not working:
<div class="metadata-container">
<div class="metadata-title">
<div>Version</div>
<div>Author</div>
<div>File</div>
</div>
<div class="metadata-content">
<div>{{ document.version }}</div>
<div>{{ document.author }}</div>
<a href=document.file download><div>{{ document.file }}</div>
</div>
</div>
The filename in format '/documents/2017/filename.txt' is in document.file variable or however it's called. How can I use it in the ? or how can I make it work?
You should write a method inside your Document model a method which should return the correct link to the file.
Similar to:
def get_document_url(self):
if self.file:
return '/documents/2017/ + self.file.name + ".txt"
And then inside html you can get it with:
<a href={{document.get_document_url}}>Link</a>
I am trying to pass what ever the user inputs into the url as keyword arguments (even if its not a real entry). When I try to assign the input name as the keyword arguments it fails.
HTML:
<p class="search">
<form method="GET" action="{% url 'job' %}" class="sidebar-form">
<div class="ogsearchbar input-group">
<input class="searchbarz" type="text" name="user_input" id="user_input" placeholder="Enter Job Number" autocomplete="off" />
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</p>
Django:
def get_job(request):
if request.method == 'GET':
formvar = request.GET['user_input']
return HttpResponseRedirect('/jobs/' + formvar)
What you want doesn't make sense. The {{ }} signs denote a context variable which is passed into the template from the server, before the template is rendered. But you're trying to use a value which is only defined when the user actually types something into the rendered page itself.
You could probably do this with some Javascript, but there doesn't seem to be much point. Drop the parameter from the URL and let the form send it in the query params, which you can access in your view as request.GET.
I decided to just go with javascript since my django code was not working with a request method when refreshing the page.