Solution 1 :

Is there any trick or idea how to pass data using form etc.?

If I understand correctly, you want to use form but a tag.

Try this:

<div class="row">
    {% csrf_token %}
      {% for folder in folder_list %}
          Folder title: <span id="titles" name="titles" >{{folder.title}}</span></p>
          Date upload: <span  id="date_upload" name="date_upload">{{folder.date_upload}}</span>

          <form action="{% url 'view_gallery' %}" method="POST" id="form1">
               <!-- This input won't be show -->
               <input type="text" idd={{folder.id}} style="display:none"> 
               <button type="submit">Open</button>
          </form>
      {% endfor %}
  </div>

Then when you hit the Open button, a POST method will be made to url view_gallery with params idd=123. (example.com/view_gallery?idd=123)

Then it will work.

But I strongly recommend you to organize URLs like this:

  • Declare <a> tag like:
<a href="{% url 'view_detail_gallery' folder.id %}">
  • Change your URL’s pattern to something like: example.com/view_gallery/:id. Declare a new pattern URL:
path('gallery/<int:id>/', view_detail_gallery, name = 'gallery_detail'),
  • Declare a new view in views.py:
@login_required(login_url='log_permission')
def view_detail_gallery(request, id):
    if request.method == 'GET':
       image = gallery_photos.objects.filter(gallery_info_id = id)
       data = {'photos':image} 
       return render(request, 'view_detail_gallery.html', data) 

You can create a new detail HTML or not because as I can see, you didn’t do it and your code is still working (Hope so).

Problem :

I’m having a trouble on how can I pass data from html into views.py based on selected row button in my html and also I want to pass through POST.As you can see the trigger to pass data is through Open button which I’ve been using href.Is there any trick or idea how to pass dat using form etc.? It would be great if anybody could figure out where I am doing something wrong. thank you so much in advance

This is similar for this problem link

enter image description here

sample.Html

  <div class="row">
    {% csrf_token %}
      {% for folder in folder_list %}
          Folder title: <span id="titles" name="titles" >{{folder.title}}</span></p>
          Date upload: <span  id="date_upload" name="date_upload">{{folder.date_upload}}</span>

          <a href="{% url 'view_gallery' %}" name="idd" value="{{ folder.id }}">Open</a>
      {% endfor %}
  </div>

views.py

@login_required(login_url='log_permission')
def view_gallery(request):
    if request.method == 'POST':
       print("makibaoh")
       idd = request.POST.get('idd')
       title = request.POST.get('title')
       date_upload = request.POST.get('date_upload')
       image = gallery_photos.objects.filter(gallery_info_id = idd)
       data = {'photos':image} 
       return render(request, 'view_gallery.html', data) 

Comments

Comment posted by John Gordon

The html template is expecting to receive a list of items called

Comment posted by Marie Loise

@John Gordon Thanks for your response the folder_list works fine it comes from another def function. I just want to pass the current data of folder_list to another def called

By