In my django models.py :
class Agent1(models.Model):
show_name = models.CharField(db_column='Show_Name', max_length=100,null=True)
exhibiting_company_name = models.CharField(db_column='Exhibiting_Company_Name', max_length=100,null=True) # Field name made lowercase.
company_website = models.CharField(db_column='Company_Website', max_length=100,null=True) # Field name made lowercase.
company_generic_email = models.EmailField(db_column='Company_Generic_Email', max_length=100,null=True) # Field name made lowercase.
class Agent2(models.Model):
show_name = models.CharField(db_column='Show_Name', max_length=100,null=True)
exhibiting_company_name = models.CharField(db_column='Exhibiting_Company_Name', max_length=100,null=True) # Field name made lowercase.
company_website = models.CharField(db_column='Company_Website', max_length=100,null=True) # Field name made lowercase.
company_generic_email = models.EmailField(db_column='Company_Generic_Email', max_length=100,null=True) # Field name made lowercase.
Like this i have around 30+ models this are just a few fields i have over 20 fields
& in my new_data.html file i have :
<form method="post"action="">{% csrf_token %}
{{ form.as_p}}
<input type="submit" name="" value="Submit">
</form>
How can i display only for eg show_name and exhibiting_company_name in my html template without creating a custom form in forms.py ?
Is there any way to call my model fields individually in a <input>
tag like this :
<form action="demo_form.asp">
Show Name: <input type="text" name="sname"><br/>
Company Name: <input type="text" name="cname"><br/>
<input type="submit" value="Submit">
</form>
?
If you only want to display those fields, that's quite easy:
<form method="post" action="">
{{ form.show_name }}<br/>
{{ form.exhibiting_company_name }}<br/>
<input type="submit" value="Submit">
</form>
But unless those are the only required fields, your form won't validate, and you won't know why since you're not displaying the error messages.
IOW, you will have to define a custom form. But really, using forms.ModelForm
, it's only a couple lines of code.
Now for something totally different: having two or more models with the same schema and named "Model1", "Model2, (...), "ModelN" is a huge design smell. If they have the same schema, they are one single model (and one single table at the db level).
If I understood problem correctly you need to show only specific fields in template. For that you can iterate over all form's fields (see docs ) and display only those with specific names:
<form method="post" action="">
{% csrf_token %}
{% for field in form %}
{% if field.name == "show_name" or field.name == "exhibiting_company_name" %}
{{ field }}
{% endif %}
{% endfor %}
<input type="submit" value="Submit">
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.