Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for field in form
#1
How can I say if field is not "album_logo" then do this else do that?
I have this code in my django form where I want to set the input for the field "album_logo" to be file and not text and to achieve this, I had to use the forloop.last since this field "album_logo" is the last field. But let's say it wasn't the last field then is there a better way to do this?

{% extends 'music/base.html' %}

{% block title %}Add a New Album{% endblock %}
{% block body %}
	<form method="post" enctype="multipart/form-data">
		{% csrf_token %}
		{% for field in form %}
			{% if not forloop.last %}
				<label for="{{ field.id_for_label }}">{{ field.label }}</label>
				<input type="text" id="{{ field.id_for_label }}" name="" value="">
			{% else %}
				<label for="{{ field.id_for_label }}">{{ field.label }}</label>
				<input type="file" id="{{ field.id_for_label }}" aceept="image/*" name="" value="">
			{% endif %}
		{% endfor %}
		<input type="submit" value="Add">
	</form>
{% endblock %}
Reply
#2
Did you try {% if field.name != 'album_logo' %} ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo only foreign key id retrive when i pass model instance to form but i want other field x64 0 3,654 Jun-03-2017, 05:58 AM
Last Post: x64

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020