def home():  
    form = CountryForm()
    if form.validate_on_submit():
        file1 = open("myfile.txt","w") 
        file1.write(form.country.data)
        file1.close() 
        return redirect(url_for('country_details'))
    return render_template('home.html',title = ' Search for a country ',form = form)
def edit_country(id):
    '''
    Edit Country
    '''
    country = Country.query.filter_by(id=id).first()
    form = CountryForm(obj=country)
    form.capital.choices += [
        (i.id, i.name)
        for i in db.session.query(City).order_by(City.name).all()
    ]

    if form.validate_on_submit():
        try:
            form.populate_obj(country)
            db.session.add(country)
            db.session.commit()
            flash('Saved successfully', 'success')
        except:
            db.session.rollback()
            flash('Error updating country.', 'danger')
    return render_template('web/edit_country.html', form=form)
def create_country():
    '''
    Create Country
    '''
    form = CountryForm()
    form.capital.choices += [
        (i.id, i.name)
        for i in db.session.query(City).order_by(City.name).all()
    ]

    if form.validate_on_submit():
        country = Country()
        form.populate_obj(country)
        db.session.add(country)
        try:
            db.session.commit()
            flash('City created correctly', 'success')
            return redirect(url_for('country.country', **request.args))
        except Exception as e:
            db.session.rollback()
            flash('Error creating country', 'danger')

    return render_template('web/create_country.html', form=form)