0

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 &raquo;</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?

1
  • try using a Django form and some input methods of bootstrap, for example <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 ) Commented Jan 31, 2016 at 21:54

1 Answer 1

2

You seem to be asking about how to pass a value in a link, which is a basic and fundamental part of Django which is well covered in the tutorial - you may want to go back and follow that again.

Firstly, you need to define a URL which captures the value:

url(r'img/(?P<pk>\d+)/$', views.addimg, name='addimg'),

then you need to accept it in your view:

def addimg(request, pk):
    ...

and use it to generate the link in the template:

<p><a class="btn btn-default" href="{% url "addimg" action.pk %}">Add Image &raquo;</a></p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.