I have used CreateView for a form, but the form is not being submitted even when it is valid. It just redirects to the same page again.I am using the form_id for from fields, and it even submitted once or twice but now it is just not working.
models.py
class Centre(models.Model):
name= models.CharField(max_length=50, blank=False, unique=True)
address = models.CharField(max_length =250)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
message="Phone number must be entered in the format: '+999999999'. Up to 10 digits allowed.")
contact = models.CharField(max_length=100, blank=False)
phone = models.CharField(validators=[phone_regex], max_length=10, blank=True)
views.py
class centre(CreateView):
fields = ('name','address','contact','phone',)
model = Centre
success_url = reverse_lazy("NewApp:logindex")
def form_valid(self, form):
form.save()
return super(centre, self).form_valid(form)
template :
<form method="POST">
{% csrf_token %}
<div class="col col-md-12">
<div class="fieldWrapper" >
{{ form.name.errors }}
<div class="form-group col col-md-3">
<label for="{{form.name.id_for_label}">Centre Name</label>
<input type="text" placeholder="Centre Name" name="name" maxlength="250" id="id_name" style="box-sizing: border-box; width:500px;">
</div>
</div>
<div class="form-group col col-md-3" style="float:right; margin-top=-80px;width=200%">
<label for="{{form.address.id_for_label}" style="margin-left:200px;width:200%;white-space:nowrap;">Centre Address</label>
<input type="text" placeholder="Centre Address" name="address" maxlength="250" id="id_address" style="width:500px; margin-left:200px;">
</div>
</div>
<br> <br><br><br>
<div class="col col-md-12">
<div class="form-group col col-md-3" style="float:right; margin-top=-80px;">
<label for="{{form.contact.id_for_label}" style="margin-left:200px;width:200px;white-space:nowrap;">Contact Person</label>
<input type="text" placeholder="Contact Person" name="address" maxlength="250" id="id_contact" style="width:500px; margin-left:200px;">
</div>
{{ user_form.non_field_errors }}
<div class="fieldWrapper" >
<div class="form-group col col-md-3" >
<label for="{{form.phone.id_for_label}" style="white-space:nowrap;">Contact Number</label>
<input type="text" name="phone" maxlength="10" id="id_phone" placeholder="Contact Number" style="width:500px";>
{{ form.phone.errors }}
</div>
</div>
</div>
<br>
<br>
<div class="col col-md-12" style="padding-left:4.5% ;padding-top:2%">
<input type="submit" value="Save" class="btn btn-primary" style=" height:30px;width:80px;padding-bottom:2em;"></input>
</div>
</form>
You have the wrong name attribute for the contact field, as a result of which it is not being submitted. At the same time, you are not outputting errors for that field.
(Note, you really should use the Django form tags to output the input elements; that would have avoided this situation, as well as pre-populating the fields when the template is redisplayed after validation.)
Related
I really don't understand how to connect these two fields or should I just create one. On my front, I have a starting date and end date I can not connect to the backend properly using django-filters Please see the below code
My filters.py
class VideoFolderFilter(FilterSet):
name = CharFilter(field_name='name', lookup_expr='icontains', widget=forms.TextInput(attrs={'class': "form-control"}))
subscription_plan = ModelChoiceFilter( label='subscription_plan', queryset=Plans.objects.all())
start_date_range = DateFromToRangeFilter(field_name='start_date_range', widget=forms.TextInput(attrs={'class': "form-control"}))
end_date_range = DateFromToRangeFilter(field_name='end_date_range', widget=forms.TextInput(attrs={'class': "form-control"}))
def get_date_range(self, start_date_range, end_date_range):
return Sample.objects.filter(sampledate__gte=start_date_range,
sampledate__lte=end_date_range)
class Meta:
model = VideoFolder
fields = '__all__'
widgets = {'start_date_range': DateInput(),}
exclude = [ 'thumbnail_link', 'link', 'name']
My views.py
#login_required
def search_videos(request):
if Subscriber.objects.filter(user=request.user).exists():
subscription = Subscriber.objects.get(user=request.user)
else:
message = "Seams like you don't have subscription with us please select one of the plans"
return redirect(reverse('main'))
video_filter = VideoFolderFilter(request.GET, queryset=VideoFolder.objects.filter(type='video').order_by('-created_date'))
videos = video_filter.qs
return render(request, 'search-videos.html', locals())
Should I change the views.py to query if yes how to date range and other fields will be included
And my search-videos.html
<div class="search-video-form-section mb-3">
<p>Use one or more filters to search below</p>
<form method="GET">
<div class="form-group mb-px-20 filter-form">
<label class="text-base">Name Contains</label>
<!-- <input type="text" class="form-control" placeholder="Keyword" /> -->
{{ video_filter.form.name }}
</div>
<div class="d-flex flex-wrap date-box">
<div class="form-group input-field">
<label class="text-base">Start Date</label>
<div class="input-group input-daterange">
{{ video_filter.form.start_date_range }}
</div>
</div>
<div class="form-group input-field">
<label class="text-base">End Date</label>
<div class="input-group input-daterange">
{{ video_filter.form.end_date_range }}
</div>
</div>
</div>
<div class="form-group filter-form">
<label class="text-base">Subscription</label>
{{ video_filter.form.subscription_plan }}
</div>
<button class="btn text-white bg-info mt-4">Search</button>
</form>
</div>
I am using Django with MySQL database. Everything is working, but the data I input will not save to the database.
This is my HTML:
<form class="space-y-8 divide-y divide-gray-200 m-2">
{% csrf_token %}
<div class="space-y-8 divide-y divide-gray-200">
<div class="pt-8">
<div>
<h3 class="text-lg leading-6 font-medium text-gray-900">
Project Information
</h3>
</div>
<div class="mt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
<div class="sm:col-span-3">
<label for="project-name" class="block text-sm font-medium text-gray-700">
Project Name
</label>
<div class="mt-1">
<input type="text" name="project-name" id="project-name"
class="shadow-sm focus:ring-gray-500 focus:border-gray-500 block w-full sm:text-sm border-gray-300 rounded-md">
</div>
</div>
<div class="sm:col-span-3">
<label for="project-location" class="block text-sm font-medium text-gray-700">
Project Location
</label>
<div class="mt-1">
<input type="text" name="project-location" id="project-location"
class="shadow-sm focus:ring-gray-500 focus:border-gray-500 block w-full sm:text-sm border-gray-300 rounded-md">
</div>
</div>
</div>
<div class="mt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
<div class="sm:col-span-3">
<label for="project-operator" class="block text-sm font-medium text-gray-700">
Operator
</label>
<div class="mt-1">
<input type="text" name="project-operator" id="project-operator"
class="shadow-sm focus:ring-gray-500 focus:border-gray-500 block w-full sm:text-sm border-gray-300 rounded-md">
</div>
</div>
<div class="sm:col-span-3">
<label for="project-start-date" class="block text-sm font-medium text-gray-700">
Start Date
</label>
<div class="mt-1">
<input type="text" name="project-start-date" id="project-start-date"
class="shadow-sm focus:ring-gray-500 focus:border-gray-500 block w-full sm:text-sm border-gray-300 rounded-md">
</div>
</div>
</div>
</div>
<div class="pt-5">
<div class="flex justify-end">
<button type="submit"
class="ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-gray-600 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500">
Save
</button>
</div>
</div>
</div>
</form>
This is my views.py
def project(request):
if request.method == 'POST':
form = ProjectForm(request.POST or None)
if form.is_valid():
form.save()
all_items = Project.objects.all
return render(request, 'project.html', {'all_items': all_items})
all_items = Project.objects.all
return render(request, 'project.html', {'all_items': all_items})
This is my forms.py:
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['project_name', 'project_location',
'operator', 'date_started']
This is my models.py
class Project(models.Model):
project_name = models.CharField(max_length=30)
project_location = models.CharField(max_length=30)
operator = models.CharField(max_length=30)
date_started = models.DateField()
I am not getting any errors, but the data I save is not saved in the database. Can anyone see what I need to fix?
In your view which directs you to html page :
def project(request):
if request.method == "POST":
form = ProjectForm(request.POST)
if form.is_valid():
form.save()
else:
print("Invalid input from user") # for debugging only, handle according to your need
form = ProjectForm()
all_items = Project.objects.all()
return render(request, 'project.html', {'form': form, 'all_items':all_items})
In your html
<form action="{% url 'url_name_for_your_project_view' %}" method="POST">
{% csrf_token %}
{{ form }}
<input type="submit" value="submit">
</form>
I'm trying to save the data from a form by POST method. When i try to save in vars at views.py it put inside none
here i show important parts of my code:
add.html:
<form method="POST">
{% csrf_token %}
<h5>AÑADIR PEDIDO</h5>
<div class="form-group">
<label for="cliente">Nombre del Cliente</label>
<input type="text" class="form-control" id="cliente" placeholder="pepito" />
</div>
<div class="form-group">
<label for="producto">Codigo del Producto</label>
<select class="form-control" id="producto">
{% for cantidades in cantidad_pedido %}
<option value="{{cantidades.Cproducto}}">
{{cantidades.Cproducto}}
</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="cantidad">Cantidad de dicho producto</label>
<input type="number" class="form-control" id="cantidad" placeholder="cantidad" />
</div>
<button type="submit" class="btn btn-dark" style="margin-bottom: 1%;">Añadir!</button>
</form>
models.py:
class Stock(models.Model):
Cproducto = models.CharField(max_length=50, primary_key=True)
cantidad = models.IntegerField()
class Pedido(models.Model):
Cpedido = models.CharField(max_length=50, primary_key=True)
Ccliente = models.CharField(max_length=50)
FechaPedido = models.DateField(default=datetime.now)
class detallePedido(models.Model):
Cpedido = models.ForeignKey(Pedido, on_delete=models.CASCADE)
Cproducto = models.ForeignKey(Stock, on_delete=models.CASCADE)
Cantidad = models.IntegerField()
views.py :
def add(request):
cantidad_pedido = Stock.objects.all()
if request.POST:
client = request.POST.get('cliente')
producto = request.POST.get('producto')
cantidad = request.POST.get('cantidad')
stock_producto = Stock.objects.get(Cproducto=producto)
if(cantidad > stock_producto.cantidad):
messages.error(request, 'Error, la cantidad es mayor')
return render(request, 'add.html', {'cantidad_pedido': cantidad_pedido})
and here one picture of the vars:
You are missing as mention above in comment
I think you are making request with id attribute but instead of id you need to do with name attribute like this
form method="POST">
{% csrf_token %}
<h5>AÑADIR PEDIDO</h5>
<div class="form-group">
<label for="cliente">Nombre del Cliente</label>
<input type="text" class="form-control" name="cliente" placeholder="pepito" />
</div>
<div class="form-group">
<label for="producto">Codigo del Producto</label>
<select class="form-control" name="producto">
{% for cantidades in cantidad_pedido %}
<option value="{{cantidades.Cproducto}}">
{{cantidades.Cproducto}}
</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="cantidad">Cantidad de dicho producto</label>
<input type="number" class="form-control" name="cantidad" placeholder="cantidad" />
</div>
<button type="submit" class="btn btn-dark" style="margin-bottom: 1%;">Añadir!</button>
</form>
and you can use both id and name but for making request you need to name attribute
I have been trying to make the date field in a form to display the current date when it renders. But I have failed to find a proper solution to this problem.
Please find below the code.
HTML File
<form class="form-horizontal" role="form" method = 'POST'>
{% csrf_token%}
<h2>New Manufacturer Details</h2>
<div class="form-group row">
<label for="createddate" class="col-sm-3 control-label">Created Date</label>
<div class="col-sm-9">
<input type="date" id="createddate" name = "createddate" class="form-control" autofocus required="true" value = '{{ createddate }}'>
</div>
</div>
<div class="form-group row">
<label for="manufname" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" id="manufname" name = "manufname" placeholder="Manufacturer Name" class="form-control" autofocus required="true" value = '{{ manufname }}'>
</div>
</div>
<div class="form-group row">
<label for="manufaddress" class="col-sm-3 control-label">Address</label>
<div class="col-sm-9">
<textarea class="form-control" id="manufaddress" name = "manufaddress" placeholder="Manufacturer Address" rows="3" required="true" value = '{{ manufaddress }}'></textarea>
</div>
</div>
<div class="form-group row">
<label for="manufcontact" class="col-sm-3 control-label">Contact Name</label>
<div class="col-sm-9">
<input type="text" id="manufcontact" name = "manufcontact" placeholder="Manufacturer POC" class="form-control" autofocus required="true" value = '{{ manufcontact }}'>
</div>
</div>
<div class="form-group row">
<label for="manufcontactnum" class="col-sm-3 control-label">Contact Number</label>
<div class="col-sm-9">
<input type="text" id="manufcontactnum" name = "manufcontactnum" placeholder="Manufacturer Contact Number" class="form-control" autofocus required="true" value = '{{ manufcontactnum }}'>
</div>
</div>
<div class="form-group row">
<label for="manufemailid" class="col-sm-3 control-label">Email Id</label>
<div class="col-sm-9">
<input type="email" id="manufemailid" name = "manufemailid" placeholder="Manufacturer Email Id" class="form-control" autofocus required="true" value = '{{ manufemailid }}'>
</div>
</div>
<div class="form-group row">
<label for="manufgst" class="col-sm-3 control-label">GST No</label>
<div class="col-sm-9">
<input type="text" id="manufgst" name = "manufgst" placeholder="Manufacturer GST Number" class="form-control" autofocus required="true" value = '{{ manufgst }}'>
</div>
</div>
<div class="form-group row">
<label for="manuflicenseno" class="col-sm-3 control-label">License No</label>
<div class="col-sm-9">
<input type="text" id="manuflicenseno" name = "manuflicenseno" placeholder="Manufacturer License Number" class="form-control" autofocus required="true" value = '{{ manuflicenseno }}'>
</div>
</div>
<div class="form-group row">
<label for="manufbank" class="col-sm-3 control-label">Bank Details</label>
<div class="col-sm-9">
<textarea class="form-control" id="manufbank" name = "manufbank" placeholder="Manufacturer Bank Details" rows="3" required="true" value = '{{ manufbank }}'></textarea>
</div>
</div>
<div class="col text-center">
<button type="submit" class="btn btn-primary" id="form-submit">Save</button>
</div>
</form> <!-- /form -->
<script>
$("#form-horizontal").validate();
</script>
Views.Py
def createmanufacturer(request):
if request.method == 'POST':
form = CreateManufacturerForm(request.POST or None)
if form.is_valid():
form.save()
else:
createddate = request.POST['createddate']
manufname = request.POST['manufname']
manufaddress = request.POST['manufaddress']
manufcontact = request.POST['manufcontact']
manufcontactnum = request.POST['manufcontactnum']
manufemailid = request.POST['manufemailid']
manufgst = request.POST['manufgst']
manuflicenseno = request.POST['manuflicenseno']
manufbank = request.POST['manufbank']
messages.success(request, ('There was an error in your form! Please try again...'))
return render(request, 'screens/createmanufacturer.html', {
'createddate' : createddate,
'manufname' : manufname,
'manufaddress' : manufaddress,
'manufcontact' : manufcontact,
'manufcontactnum' : manufcontactnum,
'manufemailid' : manufemailid,
'manufgst' : manufgst,
'manuflicenseno' : manuflicenseno,
'manufbank' : manufbank,
})
messages.success(request, ('Manufacturer Details have been submitted successfully'))
return redirect("screens:testpage")
else:
form = CreateManufacturerForm()
return render(
request = request,
template_name = 'screens/createmanufacturer.html',
context = {'form' : form}
)
forms.py
class CreateManufacturerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CreateManufacturerForm, self).__init__(*args, **kwargs)
self.fields['createddate'].initial = date.today
class Meta:
model = Manufacturer
#createddate = forms.DateField(initial=date.today)
fields = ['createddate',
'manufname',
'manufaddress',
'manufcontact',
'manufcontactnum',
'manufemailid',
'manufgst',
'manuflicenseno',
'manufbank']
models.py
class Manufacturer(models.Model):
createddate = models.DateField()
manufname = models.CharField(max_length = 255)
manufaddress = models.TextField()
manufcontact = models.CharField(max_length = 255)
manufcontactnum = models.CharField(max_length = 25)
manufemailid = models.EmailField(max_length = 200)
manufgst = models.CharField(max_length = 255)
manuflicenseno = models.CharField(max_length = 255)
manufbank = models.TextField()
manufcode = models.CharField(max_length = 255, primary_key=True, editable=False)
def __str__(self):
return self.manufname
Right now, nothing happens when the form renders. What I want is the date in the Created Date should be set to today's date. However the user could leave it as is or could select a date of his/her choice. But the requirement is that date field should be pre-populated with the current date.
Please find below the screenshot of the web form.
Web Form
To save the current use auto_now=True
class DateField(auto_now=False, auto_now_add=False, **options)¶
A date, represented in Python by a datetime.date instance. Has a few extra, optional arguments:
DateField.auto_now¶
Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override
To display current date in the form use :
form = CreateManufacturerForm(initial={'createddate': datetime.now()})
So, after a lot of frustrating hours, I was able to finally solve the problem, with help from my friend Houda. This is what I did.
views.py
In the GET portion of the code, I wrote the following.
initial_data = {
'createddate' : date.today().strftime("%Y-%m-%d"),
}
form = CreateManufacturerForm(initial = initial_data)
template.html file
I changed the following
<input type="date" id="createddate" name = "createddate" class="form-control" autofocus required="true" value = '{{ form.createddate.value }}'>
I am not sure if this is the best solution. But at least I got it to work. I believe the issue had something to do with the date format that HTML has for the
input type = 'date'
it only allows 'YYYY-mm-dd'
Thanks everyone.
Problem Description:
I have a django view and template which displays several paragraphs of data, which are retrieved from a Model. Below each one, is a <input type="file"> tag, for uploading images related to each row of data. When user uploads files, he may upload 1 to several files for one particular row of the model, or he may choose not to upload files for one or more models. I need to save these files with a foreign key to that particular row. I am not using django forms for this.
To explain once again, there are several file tags in my html, each below a particular set of text from different rows of a model. At POST, all files become collected into a list within request.FILES, with no clue as to from which input tag each was uploaded. I need to differentiate between files uploaded from different file tags, as these need to be saved to the model, referencing different rows by foreign key. How can I know which input tag a particular file in request.FILES was uploaded from?
My model:
class Procedure(models.Model):
procid = models.AutoField(primary_key=True, unique=True)
timestr = models.DateTimeField(default=timezone.now)
template = models.ForeignKey(ProcedureTemplate, on_delete=models.CASCADE, blank=True, null=True)
clinic = models.ForeignKey(Clinic, on_delete=models.CASCADE)
doctor = models.ForeignKey(doctor, on_delete=models.SET_NULL, blank=True, null=True)
customer = models.ForeignKey(customer, on_delete=models.CASCADE, null=False)
def __str__(self):
return f'{self.template} for {self.customer} on {self.timestr}'
class SectionHeading(models.Model):
procid = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=200)
fieldtype_choice = (
('heading1', 'Heading1'),
('heading2', 'Heading2'),
)
fieldtype = models.CharField(
choices=fieldtype_choice, max_length=100, default='heading1')
template = models.ForeignKey(ProcedureTemplate, on_delete=models.CASCADE, null=False)
def __str__(self):
return f'{self.name} [{self.procid}]'
class SectionText(models.Model):
procid = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=200)
widgettype_choice = (
('textarea', 'Textarea'),
('text', 'Short Text'),
)
widgettype = models.CharField(
choices=widgettype_choice, max_length=100, default='text')
heading = models.ForeignKey(SectionHeading, on_delete=models.CASCADE, null=False)
def __str__(self):
return f'{self.name} [{self.procid}]'
class SectionImage(models.Model):
procid = models.AutoField(primary_key=True, unique=True)
pic = StdImageField(upload_to="data/media/%Y/%m/%d", blank=True, variations={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
procedure = models.ForeignKey(Procedure, on_delete=models.CASCADE, null=False)
def __str__(self):
return self.pic.url
My view:
if request.method == 'POST':
print(request.POST, "\n\n")
headinglist = request.POST.getlist('qn[]')
valuelist = request.POST.getlist('ans[]')
for h, v in zip(headinglist, valuelist):
print(h, v)
print(request.FILES)
filelist = request.FILES.getlist('uploaded[]')
for f in filelist:
print(f)
report_pic = SectionImage(pic = f, procedure=proc)
report_pic.save()
print(f'Saved picture to disk: {f}')
msg = "Updated successfully"
My html:
{% for qn, ans in headingparagraph %}
<div class="row mt-4">
<div class="col-md-24">
<div class="form-group">
<label for="exampleFormControlTextarea1">{{ qn.name }}</label>
<input type="hidden" id="custId" name="qn[]" value="{{ qn.procid }}">
<textarea class="form-control reporttextarea" id="" rows="3" name="ans[]">{{ ans }}</textarea>
</div>
</div>
</div>
<div class="row mb-2">
<i class="fas fa-image fa-2x mx-2"></i> Upload Images <input type="file" class="mx-2" id="{{ qn.procid }}_upload" accept="image/*" name="uploaded[]" multiple />
</div>
{% endfor %}
This gets displayed as:
<div class="row mt-4">
<div class="col-md-24">
<div class="form-group">
<label for="exampleFormControlTextarea1">Nasal mucosa</label>
<input type="hidden" id="custId" name="qn[]" value="1">
<textarea class="form-control reporttextarea" id="" rows="3" name="ans[]">Normal nasal mucosa</textarea>
</div>
</div>
</div>
<div class="row mb-2">
<i class="fas fa-image fa-2x mx-2"></i> Upload Images <input type="file" class="mx-2" id="1_upload" accept="image/*" name="uploaded[]" multiple />
</div>
<div class="row mt-4">
<div class="col-md-24">
<div class="form-group">
<label for="exampleFormControlTextarea1">Turbinates</label>
<input type="hidden" id="custId" name="qn[]" value="2">
<textarea class="form-control reporttextarea" id="" rows="3" name="ans[]">Bilateral turbinates normal</textarea>
</div>
</div>
</div>
<div class="row mb-2">
<i class="fas fa-image fa-2x mx-2"></i> Upload Images <input type="file" class="mx-2" id="2_upload" accept="image/*" name="uploaded[]" multiple />
</div>
<div class="row mt-4">
<div class="col-md-24">
<div class="form-group">
<label for="exampleFormControlTextarea1">Middle meatus</label>
<input type="hidden" id="custId" name="qn[]" value="3">
<textarea class="form-control reporttextarea" id="" rows="3" name="ans[]">Bilateral middle meatus normal</textarea>
</div>
</div>
</div>
<div class="row mb-2">
<i class="fas fa-image fa-2x mx-2"></i> Upload Images <input type="file" class="mx-2" id="3_upload" accept="image/*" name="uploaded[]" multiple />
</div>
<div class="row mt-4">
<div class="col-md-24">
<div class="form-group">
<label for="exampleFormControlTextarea1">Inferior Meatus</label>
<input type="hidden" id="custId" name="qn[]" value="4">
<textarea class="form-control reporttextarea" id="" rows="3" name="ans[]">Inferior Meatus normal</textarea>
</div>
</div>
</div>
<div class="row mb-2">
<i class="fas fa-image fa-2x mx-2"></i> Upload Images <input type="file" class="mx-2" id="4_upload" accept="image/*" name="uploaded[]" multiple />
</div>
<div class="row ">
<div class="col-sm-12">
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-primary btn-block">Save changes</button>
</div>
<div class="col-sm-12">
</div>
</div>
When I upload files for Turbinates and Inferior Meatus, but not for others, output on command line is:
<QueryDict: {'csrfmiddlewaretoken': ['0dEBGstsSSzhOgebI2FBaHWioH7bEBmx0EPnYDE4nTrrNHZYMZCSTyId0FXAJYYR'], 'qn[]': ['1', '2', '3', '4'], 'ans[]': ['Normal nasal mucosa', 'Bilateral turbinates normal', 'Bilateral middle meatus normal', 'Inferior Meatus normal'], 'uploaded[]': ['', '']}>
1 Normal nasal mucosa
2 Bilateral turbinates normal
3 Bilateral middle meatus normal
4 Inferior Meatus normal
<MultiValueDict: {'uploaded[]': [<InMemoryUploadedFile: Screenshot from 2019-01-27 11-32-34.png (image/png)>, <InMemoryUploadedFile: Screenshot from 2019-01-26 16-25-56.png (image/png)>, <InMemoryUploadedFile: Screenshot from 2019-01-26 16-25-18.png (image/png)>, <InMemoryUploadedFile: Screenshot from 2019-01-27 11-32-34.png (image/png)>]}>
Screenshot from 2019-01-27 11-32-34.png
Saved picture to disk: Screenshot from 2019-01-27 11-32-34.png
Screenshot from 2019-01-26 16-25-56.png
Saved picture to disk: Screenshot from 2019-01-26 16-25-56.png
Screenshot from 2019-01-26 16-25-18.png
Saved picture to disk: Screenshot from 2019-01-26 16-25-18.png
Screenshot from 2019-01-27 11-32-34.png
Saved picture to disk: Screenshot from 2019-01-27 11-32-34.png
All the files get into a Querydict collection. I am unable to differentiate which section it belongs to. Is there any way I can tag specific file tags so that when the form is submitted, I can iterate the list and save each file by tagging them to an individual section heading. The database part is easy. I want to know how to build the file tags/html.
First, don't name fields with []. That's a PHP/Rubyism, there's no need for it in Django.
But the way to have different names is to give the inputs those names. The name attribute of the input is what is used as the key in the FILES/POST dictionaries.