Python Forum
open the html page from the django dropdown menu?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
open the html page from the django dropdown menu?
#1
I got this django country, state, city dropdown code and changed the mysql db and tables for my use, thisi is views.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.template import RequestContext
from django.shortcuts import render
from django.http import HttpResponse
import mysql.connector
 
 
 
# Create your views here.
 
 
#index page where the form and result is populated.
def index(request):
    cnx = mysql.connector.connect(user='root', password="rahul", host="localhost", database='quran')
    cursor = cnx.cursor()
    query="Select surah_name,id from surah"
    cursor.execute(query)
    country=cursor.fetchall()
    context={"country":country
        }
    cnx.close()
    return render(request,"index.html",context)
 
#  to populate state dropdown when country is selected.
def state_view(request,id):
    cnx = mysql.connector.connect(user='root', password="rahul", host="localhost", database='quran')
    cursor = cnx.cursor()
    query="select ayath,id from ayaths where surah_id="+str(id)
    cursor.execute(query)
    states=cursor.fetchall()
    context={"states":states
        }
    cnx.close()
    return render(request,"state.html",context)
 
#  to populate city dropdown when state is selected.
def city_view(request,id):
    cnx = mysql.connector.connect(user='root', password="rahul", host="localhost", database='quran')
    cursor = cnx.cursor()
    query="select page,id from pages where id="+str(id)
    cursor.execute(query)
    cities=cursor.fetchall()
    if cities:
        context={"cities":cities
        for citit in cities:
            print("page = ", row[2])
            }
         
    else:
        context={"cities":["",0]
        }
    cnx.close()
    return render(request,"city.html",context)
         
#  populate the Country,State,City on the Index page.
def location_view(request,conid,sid,cid):
    cnx = mysql.connector.connect(user='root', password="rahul", host="localhost", database='quran')
    cursorCon = cnx.cursor()
    cursorS = cnx.cursor()
    cursorC = cnx.cursor()
    query=["select surah_name from surah where id="+str(conid),"select ayath from ayaths where id="+str(sid),"select page from pages where id="+str(sid)]
    cursorCon.execute(query[0])
    country=cursorCon.fetchone()
    cursorS.execute(query[1])
    state=cursorS.fetchone()
         
    cursorC.execute(query[2])
    city=cursorC.fetchone()
    html="Country : "+str(country[0])+"</br>State : "+str(state[0])+"</br>City : "+str(city[0])
    cnx.close()
    return HttpResponse(html)
 
 
def location_view2(request,conid,sid):
    cnx = mysql.connector.connect(user='root', password="rahul", host="localhost", database='quran')
    cursorCon = cnx.cursor()
    cursorS = cnx.cursor()
    query=["select surah_name from surah where id="+str(conid),"select ayath from ayaths where id="+str(sid)]
    cursorCon.execute(query[0])
    country=cursorCon.fetchone()
    cursorS.execute(query[1])
    state=cursorS.fetchone()
    html="Country : "+str(country[0])+"</br>State : "+str(state[0])
    cnx.close()
    return HttpResponse(html)

this is index.html:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html>
    <head>
    {% load static %}
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="{% static 'jquery.min.js' %}"> </script>
        <script src="{% static 'main.js' %}"></script>
        <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
         
    </head>
     
     
     
    <body>
     
                 
        <form method="post" action="" name="form1" id="locationform">{%csrf_token%}
        <center>
            <table width="45%"  cellspacing="0" cellpadding="0">
                <tr>
                    <td width="75">Country</td>
                    <td width="50">:</td>
                    <td  width="150">
                      <select name="country" onChange="getState(this.value)">
                        <option value="Select Country">Select Country</option>
                        {%for con in country%}
                        <option value="{{con.1}}">{{con.0}}</option>
                        {%endfor%}
                      </select>
                    </td>
                </tr>
                <tr>
                    <td>State</td>
                    <td width="50">:</td>
                    <td >
                        <div id="statediv">
                            <select name="state">
                                <option value="">Select State</option>
                            </select>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>City</td>
                    <td width="50">:</td>
                    <td>
                        <div id="citydiv">
                            <select name="city" >
                                <option value="">Select City</option>
                            </select>
                        </div>
                    </td>
                </tr>
            </table>
            <input type="submit" value="Submit" onclick="result()">
            <div id="result"></div>
           
        </center>
 
        </form>
 
 
 
         
    </body>
</html>
and this is city.html:
1
2
3
4
5
<select name="city">
    {%for city in cities%}
         <option value="{{city.1}}">{{city.0}}</option>
    {%endfor%}
</select>
when i click the button from index.html it prints the value of all three dropdown menu as shown in the image bellow how i can do this when i click the button should open the the third drop down menu which is a html page.
this is the image of dropdown window when i click the button:
https://ibb.co/GQxC4Yc
Reply
#2
I made this modification to the views.py:
1
2
3
4
5
6
7
cursorC.execute(query[2])
city=cursorC.fetchone()
html="Country : "+str(country[0])+"</br>State : "+str(state[0])+"</br>City : "+str(city[0])
hpage= str(city[0])
webbrowser.open_new_tab(hpage)
cnx.close()
return HttpResponse(html)
now when i click the button it opens the html file but the problem is not opening as a local web serve file, adding the https//www.name_of_html_file assuming it it's a remote web page, not a local file.
Reply
#3
finally the solution was i added this two lines to views.py:
1
2
hpage= str(city[0])
webbrowser.open('http://example.com/'+hpage)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebElements of an HTML page Nik1811 2 1,571 Mar-14-2024, 12:39 PM
Last Post: Nik1811
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 1,463 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 1,895 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Getting barplots from the dropdown menu v_mn 0 1,242 Sep-14-2022, 10:48 AM
Last Post: v_mn
  simple html page with update data korenron 3 4,787 Nov-15-2021, 09:31 AM
Last Post: jamesaarr
  Populate Dropdown list from Database TommyAutomagically 4 8,320 Nov-02-2021, 05:23 PM
Last Post: ndc85430
  reading html and edit chekcbox to html jacklee26 5 4,424 Jul-01-2021, 10:31 AM
Last Post: snippsat
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 2,536 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Open PDF in certain page ammann 4 28,181 Jun-01-2018, 10:44 AM
Last Post: ammann

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020