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:
# -*- 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:
<!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:
<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:
    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:
    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 264 Mar-14-2024, 12:39 PM
Last Post: Nik1811
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 639 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 877 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Getting barplots from the dropdown menu v_mn 0 692 Sep-14-2022, 10:48 AM
Last Post: v_mn
  simple html page with update data korenron 3 2,585 Nov-15-2021, 09:31 AM
Last Post: jamesaarr
  Populate Dropdown list from Database TommyAutomagically 4 4,349 Nov-02-2021, 05:23 PM
Last Post: ndc85430
  reading html and edit chekcbox to html jacklee26 5 3,020 Jul-01-2021, 10:31 AM
Last Post: snippsat
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 1,918 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Open PDF in certain page ammann 4 26,412 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