Another basic question that I'm afraid of, which I'm struggling with. I have been browsing through various Django documentation pages, as well as browsing this site. The only thing I found here was in 2013, which suggested setting up my own filter template.
Anyway, I'm trying to create my own form instead of using my own way to create Django through {{form}}. It's just that I can control the way the form is presented.
I have developed various ways to access the necessary information, such as (inside my element in a form loop);
- item.help_text
- item.label_tag
- item.id_for_label
I am trying to determine the type of an item so that I can use the correct input type, however I am trying my best to train what item.xxxx should be. Since this is correctly displayed through {{form}}, I make the assumption that this information is available somewhere in the form, just trying to figure out how to access it so that I can determine the correct input type. I do this manually, so I can use the correct Bootstrap styles to display input fields.
Any help would be appreciated (or just pointed in the right direction). I am very new to this, so I apologize for my most basic questions, itβs difficult, without knowing someone, I can just ask these questions.
Hi
Wayne
Not sure if you need this, but here is some code;
the form:
class NewsForm(ModelForm):
class Meta:
model = News_Article
exclude = ('news_datetime_submitted', 'news_yearmonth', )
labels = {
'news_title': _('Enter News Title'),
}
help_texts = {
'news_title': _('Enter a title to give a short description of what the news is.'),
}
error_messages = {
'news_title': {
'max_length': _("News title is too long."),
},
}
( POST, , Django, POST - )
def create(request, dataset):
if dataset not in ['news', 'announcement']:
return HttpResponseRedirect(reverse('pages'))
rDict = {}
if request.method == 'POST':
if dataset == "news":
form = NewsForm(request.POST)
elif dataset == "announcement":
form = AnnouncementForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/home/')
else:
pass
else:
announcement = get_announcement()
if not announcement == None:
rDict['announcement'] = announcement
if dataset == "news":
rDict['form'] = NewsForm()
rDict['branding'] = {'heading': 'Create News Item', 'breadcrumb': 'Create News', 'dataset': 'create/' + dataset + '/'}
elif dataset == "announcement":
rDict['form'] = AnnouncementForm()
rDict['branding'] = {'heading': 'Create Announcement', 'breadcrumb': 'Create Announcement', 'dataset': 'create/' + dataset + '/'}
rDict['sitenav'] = clean_url(request.path, ['"', "'"])
rDict['menu'] = Menu.objects.all().order_by('menu_position')
return render(request, 'en/public/admin/admin_create.html', rDict)
<form action="/siteadmin/{{ branding.dataset }}" method="post">
{% csrf_token %}
{% for item in form %}
<div class="row">
<div class="col-xs-2 col-md-2">
</div>
<div class="col-xs-4 col-md-4">
<div class="panel-title pull-right">
{% if item.help_text %}
<img src="/static/images/info.png" height="20" width="20" aria-hidden="true" data-toggle="popover" title="{{ item.help_text }}"> 
{% endif %}
{{ item.label_tag }}
</div>
</div>
<div class="col-xs-4 col-md-4">
<div class="input-group">
<input type="{{ item.widget }}" class="form-control" placeholder="" aria-describedby="{{ item.id_for_label }}">
</div>
</div>
<div class="col-xs-2 col-md-2">
{% if forloop.last %}
<input type="submit" value="Submit" />
{% endif %}
</div>
</div>
{% endfor %}
</form>