Ok, now I know how to display images on ONE /myurl/ in Django as a list (YEAH!)
But how can I display ONE Image per url with a button to click to the next image etc.
So basically I would want to come to the first /urlstart/:
text and a next button.
brings user to e.g. /urlstart/1
There the first image of a List is displayed and below that a button.
That brings user to /urlstart/2
Here the next image of a list is displayed.
etc.
How does the url.py regex part have to look like so this is possible?
How does the view has to be tweaked to get 1 List of Images but multiple urls?
And finally if anybody has implemented this with a down loadable and installable module/library, like e.g. photologue (which unfortunately did not work for me) or any better idea than that I would be really happy to get some insights on how to make this fast and efficient.
Thanks so much to everybody who takes the time to answer!
URL regex could look something like this:
url(r'^urlstart/(?P<image_id>\d*)/?$', 'urlstart', name='urlstart')
View code could look something like this:
def urlstart(request, image_id=0):
if image_id == 0:
image = None
else:
image = get_object_or_404(Image, image_id)
next_image = image_id + 1
return render_to_response('template.html', locals(), context_instance=RequestContext(request)
Template code:
{% if image %}
<img src={{ image.url }}>
Next
{% else %}
Put your text here. See first image by clicking here
{% endif %}
Obviously this is simplified. For example, you may want to check to see if there is an image to come next.
Hope this helps.
Take a look at Pagination which will provide you with Next/Previous links, etc.
Also, it would probably be a good idea to use permalinks and pass the next/previous image in from the view instead of hard-coding the URL into the template.
Related
I have the following snippet of HTML:
On my django webpage, i get a list that looks as follows:
Each "Part" anchor corresponds with a Part object in Python. Now, I'd like to make it so that when the user clicks on "Part_2", the datapoints of that Part are shown just below it.
On the image, the datapoints of the first part are shown within the nested list. I hard coded this. I looked into calling a python function with an argument from the template, but that is not possible.
What would be an easy way to make this happen?
You can achieve this on the client side. First, create a detail element in your HTML and put a summary and the tags that you need (in this case, I'll be using a <p></p>) as childs:
<details>
<summary>Part 1</summary>
<p>Epcot is a theme park at Walt Disney World Resort featuring exciting attractions, international pavilions, award-winning fireworks and seasonal special events.</p>
</details>
What's inside the summary tag will represents the title and below that goes the hidden text that can be accessed by clicking in the arrow on the side of the title.
Then, make sure you filter in your function in views.py the parts by part_1 and part_2 and then pass them into your context. Lastly, render the items like this:
<details>
<summary>Part 1</summary>
{% for part in part_1 %}
<p> {{ part.tag }}</p>
{% endfor %}
</details>
I am creating a web store with HTML and Bottle (Python). I know how to pass data between HTML and Python, as well as how to write Python code inside HTML (reference: http://karrigell.sourceforge.net/en/pythoninsidehtml.html).
What I struggle with is writing Python code inside the reference that gets passed from HTML to Python.
I load images from a database since I have a lot of them. I set and pass the variables image_name(index) to HTML like this:
#get('/store')
def showStore():
images = models.images() # reads from database
data = ', number_of_images={}'.format(len(images))
for (id, title, path) in images:
data += ', image_name{}={}, '.format(id, path)
data += 'image_title{}={}'.format(id, title)
return template('store.html' + data)
The relevant part of store.html:
<!-- Portfolio Gallery Grid -->
<div class="row">
<% for i in range(1, number_of_images+1): %>
<div class="column">
<div class="content">
<img src="static/images/{{image_name<%i%>}}.jpg">
<h3> {{image_title<%i%>}} </h3>
</div>
</div>
<% end %>
</div>
Since Bottle is not that well known: {{image_title}} in HTML gets the value passed in from the template (html, variables). Block <% %> enables you to write Python code inside it.
Everything worked properly when I didn't use indexing of image properties inside the HTML, so HTML and the for loop do work, the problem is with this part:
{{image_title<%i%>}}
<%i%> should in my opinion create a variable image_titlei but apparently it doesn't.
I get a template not found error, probably because the variables I am passing in (from the .py file) do not exist in the HTML.
The reason I would very much like to do this dynamically instead of hardcoding every image is that I have a lot of those images and every image has a whole table of relevant data I left out here because I would retrieve it in the same way as image's name and title.
I would appreciate any help with proper combining Python inside HTML with variables passed in since I really want to do it dynamically
There are a rather large number of things wrong here.
Your template not found error is actually nothing to do with the strange manipulations you are doing within that template. It is because you are concatenating the template name with the data string in your call to template() in the last line of your Python code. I don't know bottle, but I'm absolutely positive the data needs to be a second parameter to that function:
return template('store.html', data)
As for the rest, there's absolutely no need to do anything that you are doing there. Just pass the images as a list, and iterate through that list in the templates. Dynamic variables are always a bad idea, and that applies just as much to templates as it does to standard Python code.
(Although note that you don't even create any dynamic variables; you just have a single string, formatted to look like a set of variable assignments. But that doesn't make it actually assign any variables anywhere.)
I have a CSS element thats been declared something like :
span.green_box {width:5px, height:7px ....and other specifications.}
I really dont know any CSS and its not my code. However I do understand that what it does is that it is creating a green coloured box of the given dimensions.
Now in my Django-template I want to display a text on mouseover on this green_box. Just displaying a text would not have been a problem because its done something like this in my django template :
<span class="green_box" title="I am green."></span>
And this works. But I have to refactor it to display something like I am Green and today is 28-6-2013
So for this I have a custom tag, get_todays_date that returns the current date as a string.
And I want to do something like,
<span class="green_box" onmouseover="I am Green and today is %s" % {{get_todays_date}}></span>
My intention is to reproduce the value returned by get_todays_date into %s. I wrote it to describe the structure. This obviously doesn't work. I've looked over on other similar questions but their solution don't work. Most are for mouse over on an image, and a predefined text. So how can I display the text including the value returned by get_todays_date on mouseover on the green_box?
There is no string formatting in django templates as there is in python. There is also a template tag which is able to retrieve todays date (https://docs.djangoproject.com/en/dev/ref/templates/builtins/#now). Hope this leads into the right direction.
You could try something like this:
<span class="green_box" onmouseover="I am Green and today is {% now "d-n-Y" %}">
In my django app's index.html (which I consider as the home page of the site)I am giving links to various locations in the website.When the user reaches one of the locations,he should find a link there, to get back to the home page.So I created a base.html to be inherited by all the pages including index.html.In the base.html,I am providing these links.
base.html
...
Archives
<br/>
Reports
<br/>
Home
<br/>
..
My problem is that ,when I am on index page(home) ,I don't want to display the home link.Is this possible to do so in the template,using an {% if %} condition?
Currently I am not passing any parameter in the view
#login_required
def index(request, template_name):
print 'index()::template=',template_name
return custom_render(request, {},template_name )
def custom_render(request,context,template):
req_context=RequestContext(request,context)
return render_to_response(template,req_context)
Firstly, you can use the django render shortcut to avoid having to use the long-winded custom_render.
from django.shortcuts import render
#login_required
def index(request, template_name):
return render(request, template_name,extra={})
You can be cheeky and in your base.html do something like:
{% url home as home_url %}
{% if request.path != home_url %}Home{% endif %}
to quickly check if the current page is the home page.
If it might get more complicated in the future, you can use a template tag to check whether or not the current URL you are on matches another URL. Here is a template tag I have used before to check if a menu link is currently active:
https://gist.github.com/2049936
This isn't exactly what you asked for, but an alteratively way to do this which I often much prefer is to use JavaScript to either hide or change the color of links to the current page. For example, if you were using jQuery:
// Assumes relative paths in a elements
$('a[href="' + window.location.pathname + '"]').each(function (index, element) {
$(element).addClass('someClass');
});
someClass could be a CSS rule to set the 'display' attribute of that element to 'none', or to simply make the link look like plain text, and behave like plain text when mousing over or clicking on it.
This has two possible advantages:
it prevents your template from becoming too convoluted, and from being difficult to maintain if you start arbitrarily adding nav elements
you have the ability to keep your nav consistent between pages, which in my opinion is a good design decision
It's no silver bullet, but I think it's important to recognise that sometimes these things aren't necessarily best done in templates. I think of my django templates first and foremost as a way of structuring data. There will always be some element of presentation in them, but I try to remove that as much as possible. Others will disagree.
<form action='/[0-9]+' method="POST">
<input type="submit" value="delete question" name="delete">
</form>
what above is the html template I am using for the appengine project. Besides that, i created a web request handler class to handle this request. ('/[0-9]+',QuestionViewer), it is supposed to catch any url in digits. However, turns on that after I click on the delete button above, my page is directed to some url like main/[0-9], I dont know if I can use regex in the django template, or is there a away that my QuestionViewers class can catch the url in digits? since my url associated with the html page is dynamic, like the parts after / ,like /13,are changing accordingly and I cant do that only works for page 13 but not for /14 or something like these. Hope I make it clear. any helps? Thank you a lot.
That doesn't really make sense. You want to submit your form to a regex rule? What would it match against?
No, the form needs to submit to a specific url. Right now, it's trying to submit to /[0-9]+
If I understand what you are saying, and you want to submit from a url such as /13/ to your QuestionViewer at /[0-9]+, simply submit without the action attribute or set it to "" to post to the current url.
Note that if you want to use the digit captured in your regex, you need to surround your regex in parenthesis such as '/([0-9]+)/$', QuestionViewer or use a named regexp /(?P<id>[0-9]+)/$ to pass in an argument of id equal to the matched regex to QuestionViewer.
http://docs.djangoproject.com/en/1.0/topics/http/urls/#how-django-processes-a-request
The value of the action attribute must be a valid URL.
I think what you want is to generate an actual number for the action url; a number that is the number of the question that you want to delete. For example:
<form action="/1234" method="POST">
You will need to change your code to make sure you do this.