program tip

.css 파일 (flask / python)을 선택하지 않는 애플리케이션

radiobox 2020. 10. 26. 07:52
반응형

.css 파일 (flask / python)을 선택하지 않는 애플리케이션


외부 스타일 시트로 스타일을 지정하려는 템플릿을 렌더링하고 있습니다. 파일 구조는 다음과 같습니다.

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /styles
        - mainpage.css

mainpage.html은 다음과 같습니다.

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "../styles/mainpage.css">
    </head>
    <body>
        <!-- content --> 

내 스타일은 적용되지 않습니다. HTML이 내가 렌더링하는 템플릿이라는 사실과 관련이 있습니까? 파이썬은 다음과 같습니다.

return render_template("mainpage.html", variables..)

여전히 템플릿을 렌더링 할 수 있기 때문에이 정도만 효과가 있다는 것을 알고 있습니다. 그러나 html의 "head"태그 내의 "style"블록에서 외부 파일로 스타일링 코드를 이동하려고하면 모든 스타일링이 사라지고 html 페이지 만 남았습니다. 누구든지 내 파일 구조에 오류가 있습니까?


Flask 초기화 중에 특별히 재정의하지 않는 한 '정적'폴더 설정 (css / js 파일 용)이 필요합니다. 나는 당신이 그것을 무시하지 않았다고 가정합니다.

CSS의 디렉토리 구조는 다음과 같아야합니다.

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /static
        /styles
            - mainpage.css

/ styles 디렉토리는 / static 아래에 있어야합니다.

그런 다음 이렇게

<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">

Flask는 이제 static / styles / mainpage.css에서 css 파일을 찾습니다.


jinja2 템플릿 (플라스크가 사용하는)에서

href="{{ url_for('static', filename='mainpage.css')}}"

static파일은 보통입니다 static다르게 구성하지 않는 한,하지만, 폴더.


나는 여러 스레드를 읽었지만 사람들이 설명하고 경험 한 문제를 해결하지 못했습니다.

나는 conda에서 벗어나 pip를 사용하여 python 3.7로 업그레이드하려고 시도했으며 제안 된 모든 코딩을 시도했지만 수정되지 않았습니다.

그리고 여기에 이유 (문제)가 있습니다.

기본적으로 python / flask는 다음과 같은 폴더 구조에서 정적 및 템플릿을 검색합니다.

/Users/username/folder_one/folder_two/ProjectName/src/app_name/<static>
 and 
/Users/username/folder_one/folder_two/ProjectName/src/app_name/<template>

Pycharm (또는 기타)에서 디버거를 사용하여 직접 확인하고 앱 (app = Flask ( name )) 의 값을 확인하고 teamplate_folder 및 static_folder를 검색 할 수 있습니다.

이 문제를 해결하려면 다음과 같이 앱을 만들 때 값을 지정해야합니다.

TEMPLATE_DIR = os.path.abspath('../templates')
STATIC_DIR = os.path.abspath('../static')

# app = Flask(__name__) # to make the app run without any
app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)

TEMPLATE_DIR 및 STATIC_DIR 경로는 파일 앱이있는 위치에 따라 다릅니다. 제 경우에는 src 아래의 폴더에있는 사진을보세요.

원하는대로 템플릿과 정적 폴더를 변경하고 앱에 등록 할 수 있습니다 = Flask ...

사실, 나는 폴더를 엉망으로 만들 때 문제를 경험하기 시작했고 때로는 일하지 않았을 때도 있습니다. 이것은 문제를 한 번에 해결합니다.

HTML 코드는 다음과 같습니다.

<link href="{{ url_for('static', filename='libraries/css/bootstrap.css') }}" rel="stylesheet" type="text/css" >

이 코드

여기에 폴더 구조


I'm running version 1.0.2 of flask right now. The above file structures did not work for me, but I found one that did, which are as follows:

     app_folder/ flask_app.py/ static/ style.css/ templates/
     index.html

(Please note that 'static' and 'templates' are folders, which should be named exactly the same thing.)

To check what version of flask you are running, you should open Python in terminal and type the following accordingly:

import flask

flask --version


The flask project structure is different. As you mentioned in question the project structure is the same but the only problem is wit the styles folder. Styles folder must come within the static folder.

static/styles/style.css

I'm pretty sure it's similar to Laravel template, this is how I did mine.

<link rel="stylesheet" href="/folder/stylesheets/stylesheet.css" />

Referred: CSS file pathing problem

Simple flask application that reads its content from a .html file. External style sheet being blocked?

참고URL : https://stackoverflow.com/questions/22259847/application-not-picking-up-css-file-flask-python

반응형