Python Forum
for field in form - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: for field in form (/thread-28334.html)



for field in form - mp3909 - Jul-14-2020

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 %}



RE: for field in form - scidam - Jul-17-2020

Did you try {% if field.name != 'album_logo' %} ?