When my flask web page is in “GET” method, it couldn't connected to any static files it normally does in “POST” method

▼魔方 西西 提交于 2020-08-10 19:23:48

问题


===============SOLUTION HAD BEEN ADDED BELOW==================

I have the python codes that let users edit their comments on a post page.

When the users click on this button below:

<a href="{{ url_for('blog_posts.blog_info_update', blog_validated_id=post2.blog_post_id, blog_info_id=post2.blog_info_id) }}"><button class="btn btn-light btn-sm text-muted ">Edit</button></a>

They will request a "GET" page. Then when they finished editing and post the comment (or form2), they will request a "POST" version of another page:

@blog_posts.route('/<int:blog_validated_id>/<int:blog_info_id>/update', methods=['GET', 'POST'])
@login_required
def blog_info_update(blog_validated_id, blog_info_id):

    blog_view = BlogPost.query.get_or_404(blog_validated_id)
    blog_info_update = BlogInfo.query.get_or_404(blog_info_id


    form2 = BlogInfoForm()

    if form2.validate_on_submit():
        blog_info_update.text=form2.text.data
        db.session.commit()
        return redirect(url_for('blog_posts.blog_view', blog_validated_id=blog_validated_id, form2=form2)
                                

    elif request.method == 'GET':
        form2.text.data = blog_info_update.text


    return render_template('blog_view.html', blog_validated_id=blog_validated_id, form2=form2, 
                                blog_info_id=blog_info_update.blog_info_id)

However, when I went to this "GET" version of the page, I see that some codes ( those that I wrote in the static files and connected to my HTML file ) are not showing their functions. I think there is a problem with this "GET" mode that is interfering with my static files (shown below) and I would greatly appreciate if you could help me fix this problem:

The codes below are the one I'm linking my HTML file with:

 {% extends "base.html" %}

{% block content %}   
<link rel="stylesheet" href="../static/css/blog_view.css">
    <script type="text/javascript" src="../static/javascript/blogview.js"></script>

{% endblock %}

An image that shows my problem:

(As @gelonida suggested below, there is a problem with the relative paths or /45/static/css/blog_view.css. In contrast, /static/css/base.css works fine)

Thank you!


回答1:


The filepath to the static folder is the issue. You can try this

<link rel="stylesheet" href="{{ url_for('static/css', filename='blog_view.css') }}">
    <script type="text/javascript" src="{{ url_for('static/javascript', filename='blogview.js') }}"></script>



回答2:


I might be wrong, but at least worth looking at.

I read your question rather quickly. I might have more time in a few hours or tomorrow.

I assume this is your problem: '/<int:blog_validated_id>/<int:blog_info_id>' is working and everything is displayed fine

On the other hand '/<int:blog_validated_id>/<int:blog_info_id>/update' is not working

If this is the case, then the issue is that your template contains relative urls.

In the first case: "/<int:blog_validated_id>/static/css/blog_view.css"

whereas in the second case it would (because of the added /update) correspond to "/<int:blog_validated_id>/<int:blog_info_id>/static/css/blog_view.css"




回答3:


The problem was solved when I twitched a little bit of @Prakhar Gupta's code from above:

     <link rel="stylesheet" href="{{ url_for('static', filename='css/blog_view.css') }}">
    <script type="text/javascript" src="{{ url_for('static', filename='javascript/blogview.js') }}"></script>

Other images from the static file will have to be written in the same way I wrote for the href like above in order to not meet relative URLs issue as @gelonida said.

P.s/ I want to thanks @gelonida and @Prakhar Gupta for helping me to come up with my own solution :)



来源:https://stackoverflow.com/questions/62516834/when-my-flask-web-page-is-in-get-method-it-couldnt-connected-to-any-static-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!