简体   繁体   中英

How to pass dynamic url of selected value from dropdwonlist in django

I have a dropdwon list..when i click on that i got a value like some name..now i want that name pass as a url in django

Views.py

@csrf_exempt
def forecasting(request):
    Category = category.objects.all()

    return render(request, "myapp/casting.html",{'Category':Category})

@csrf_exempt
def Prediction(request,name):
    Category1 = category.objects.get(Category=name)

    print(Category1.Category)
    return render(request, "myapp/casting.html", {'Category1': Category1})

Models.py

class category(models.Model):
    Category = models.CharField(max_length=50)

    def __str__(self):
        return self.Category

Html file

<form  id="myform" method="post" name="myform" action="/Prediction/{{Category1.Category}}">

                    <div class="col-lg-3 col-md-3">
                        <h3> Select Category</h3>
                    </div>
                    <div class="col-lg-3 col-md-3">
                        {%csrf_token%}
                        <select id="Category" name="catagery_id" onchange="myform.submit()">
                            <option value="">Select</option>
                            {% for Category in Category %}
                            <option value="{{ Category.Category }}">{{ Category.Category }}</option>

                            {% for message in messages %}
                            <div>
                                <p>{{message|safe}}</p>

                            </div>
                            {% endfor %}

                            {% endfor %}
                        </select>
                    </div>
                    <ul class="messages">
                        {% for message in messages %}
                        <p
                                {% if message.tags %} class="{{ message.tags }}" {% endif %}>{{ message }}</p>
                        {% endfor %}
                    </ul>
                    <div class="col-lg-3 col-md-3">

                    </div>


                </form>

I have no idea how to do this..because value come when i submit the form..i am new to this please help me out

I think you want to pass that value which is selected in dropdown in action of form, You can try this using jquery

$("#Category").on("change",function(){
  action_url = '/Prediction/'+$(this).val()
  $('#myform').attr('action', action_url); 
  $('#myform').submit()
})

in this case you have to submit form from jquery by removing onchange="myform.submit()"

Replace following codes

{% for Category in Category %}
 {% if Category.Category == Category1.Category %}
   <option value="{{ Category.Category }}" selected>{{ 
    Category.Category } 
   </option>
 {% else%}
   <option value="{{ Category.Category }}">{{ Category.Category } 
   </option>
 {% endif %} 

 {% for message in messages %}
 <div>
 <p>{{message|safe}}</p>

  </div>
  {% endfor %}

 {% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM