Flask ile HTML çağıramıyorum

Merhabalar,

app.py:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')

def hello():

  return render_template('index.html')

if __name__ == '__main__':

  app.run()

index.html:

<!DOCTYPE html>

<html>

  <head>

    <title>Hello World</title>

  </head>

  <body>

    <h1>Hello World!!</h1>

  </body>

</html>

http://127.0.0.1:5000/ 'ye girince:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Diyor. Neden olabilir?

Şurada diyor ki

Flask will look for templates in the templates folder.

Sizde de öyle mi acaba? Bir de yine aynı sayfada anlatılan “debug” moduna da bir bakabilirsiniz.

html dosyanızı varsayılan klasöre. koymamanızdan kaynaklanıyor. Bulunduğun dizine ‘templates’ adlı bir klasör oluşturup html dosyanı oraya atarak tekrar dener misin?

Eğer istersen ‘grata’ diye bir klasör oluşturup flask nesnesi oluşturuken şöyle bir şey yaparak da sorunu çözebilirsin.

app = Flask(__name__, template_folder= "grata")

Hatta klasör oluştumayıp şöyle dahî yapabilrisin:

app = Flask(__name__, template_folder= ".")

Ancak standart olarak ‘templates’ adlı bir klasör oluşturup içine html dosyalarını koymak yaygındır.

1 Beğeni

Hocam dediğinizi yaptım fakat yine olmadı.

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')

def hello():

  return "sa"

@app.route('/admin')

def adm():

  return render_template("templates/login.html")

if __name__ == '__main__':

  app = Flask(__name__, template_folder= "templates")

  app.run()

templates/login.html:

<input type="textarea" placeholder="Enter your username">

<br>

<br>

<input type="password" placeholder="Enter your username">

<br>

<br>

<input type="submit" value="Log In">

<br>

<br>

if bloğunun altına sadece app.run() koymayı dener misin? if bloğunun altındaki ilk satırı sil yani. Dosya ismi “templates” ise template_folder’ı girmene gerek yok. En üste sadece nesneyi oluştursan yeter.

Şu an templates/templates/login.html diye bir dosya arıyor

2 Beğeni

Hocam halen aynı.

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')

def hello():

  return "sa"

@app.route('/admin')

def adm():

  return render_template("login.html")

if __name__ == '__main__':

  app = Flask(__name__, template_folder= "templates")

  app.run()

Bu seferde:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

Diyor.

Iki tane Flask yaratiliyor.

1 Beğeni

Yukarıda bahsettiğim gibi if bloğunun altındaki ilk satırı silmen lazım.

Hocam halen aynı.

from flask import Flask, render_template

app = Flask(__name__, template_folder= "templates")

@app.route('/')

def hello():

  return "sa"

@app.route('/admin')

def adm():

  return render_template("login.html")

if __name__ == '__main__':

  app.run()

Hata:

Exception has occurred: TemplateNotFound       (note: full exception trace is shown but execution is paused at: adm)
login.html
  File "C:\Users\emiry\Desktop\Flask\newproj\venv\app.py", line 11, in adm (Current frame)
    return render_template("login.html")

Nerede yanlış yapıyorum anlamadım. Düzelttiğiniz satırı belirterek, doğru kodu atabilir misiniz?

Nesneyi sadece şu şekilde oluşturabilir misin?

app = Flask(__name__)

Halen aynı hocam.

20 karakterr

“login.html” dosyası “C:\Users\emiry\Desktop\Flask\newproj\venv\templates\login.html” şeklinde kayıtlı, değil mi?

  • venv/
    • app.py
    • templates/
      • login.html

Evet.

20000000000000000

venv altındakileri newporj altına getirip venv’ i silip tekrar dener misin? Bir de decoratorler altındaki satır boşluğunu sil. Bir de çalıştırırken,

flask run

komutuyla çalıştır.

1 Beğeni

Bu sefer oldu hocam, teşekkürler.

1 Beğeni