I am creating a site using python and django. I have 2 models, Action and Image:
class Action(models.Model):
name = models.CharField("Action name", max_length=50)
keywords = models.CharField("Keywords", max_length=50)
object = models.CharField("Object", max_length=50, blank=True, null=True)
uploadDate = models.DateField("Date", default=get_current_date)
UploadedBy = models.CharField("UploadedBy", max_length=50, default="")
class Image(models.Model):
image = models.FileField(upload_to=get_upload_file_name, default="")
action = models.ForeignKey(Action)
I have created a combined form which allows users to create one action and add an image. Now I am trying to make a form which users can upload an image only, relating to an action, following on from the current page they are viewing the action.
Below shows the html template for ActionDetails:
<ul>
<li><h3> Name: </h3> {{ action.name }} </li>
<li><h3> Keywords: </h3> {{ action.keywords }} </li>
<li><h3>Uploadedby: </h3> {{ action.UploadedBy }}</li>
{% if action.object %}
<li><h3>Object:</h3> {{ action.object }}</li>
{% endif %}
</ul>
<p><a class="btn btn-default" href="img">Add Image »</a></p>
{% if image.image %}
<p><h3>Image:</h3><img src= "/static/app/{{image.image}}" width='300'/></p>
{% endif %}
I would like that once the form opens (btn 'Add Image') is clicked, the action.id is passed to the ImageForm page.
views.py:
def addimg(request):
if request.method == "GET":
form = ImageForm;
return render(request,'app/createForm.html', { 'form':form })
elif request.method == "POST":
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/actions')
else:
form = ImageForm;
return render(request, 'app/createForm.html', { 'form':form })
Is there a way of doing this?
<input id="inputFile" multiple="" type="file">it's very easy for select a file in computer and upload it to specific folder in your project ( site )