0

===============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: enter image description here

(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!

Upchanges
  • 310
  • 2
  • 14

3 Answers3

2

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>
Prakhar Gupta
  • 108
  • 1
  • 7
  • Hi Prakhar Gupta, thank you for helping me! But your codes still don't fix my problem... I got the error with the variable "filename=" . – Upchanges Jun 23 '20 at 09:32
  • Hi @Prakhar Gupta, I met a new problem and I would greatly appreciate it if you could help me with it: https://stackoverflow.com/q/62699111/13097721 Also, thank you for helping me a lot! – Upchanges Jul 03 '20 at 00:18
  • Hi @Prakhar Gupta, I solved it by looking at this question: https://stackoverflow.com/questions/18290142/multiple-forms-in-a-single-page-using-flask-and-wtforms :) But, thank you! – Upchanges Jul 03 '20 at 07:35
1

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"

gelonida
  • 5,327
  • 2
  • 23
  • 41
  • Hi @gelonida, thank you so much for taking the time to help me! I will look in this matter of relative urls :) – Upchanges Jun 23 '20 at 09:51
  • you can try with absolute imports or with a template variable like static_prefix or with Prakhar Gupta's suggestion. After the changes see whether both urls behave the same way or not. Don't fogtet to activate debug mode in you web browser (and look at the logs) you should see whether what url's are requested. – gelonida Jun 23 '20 at 10:07
  • Hi @gelonida, I solved the problem by twitching Prakhar Gupta's code and from your suggestion. Thank you so much! :) (I also posted the solution in the answers) – Upchanges Jun 23 '20 at 13:28
  • 1
    Just for info: stack overflow added a new (in my opinion confusing) button (the "say thanks" button) As far as I understand (others may correct me, but this is my understanding). the best way to say "thank you" is still to up-vote answers if they solve an issue or if they contributed to solving an issue. If I understand the "thank you"-button gives you a warm fuzzy feeling, whereas an upvote gives you reputation, that can be used for bounties or additional privileges. But again I somehow missed a clear communication about this button – gelonida Jun 23 '20 at 13:59
  • I agree with you. I would prefer reputation over "the feeling" :) – Upchanges Jun 23 '20 at 14:21
  • 1
    @Prakhar Gupta will appreciate in particular as a new contributor, because reputation 50 allows to comment a little better and he's now a few points closer – gelonida Jun 23 '20 at 14:23
  • Hi @gelonida, I met a new problem and I would greatly appreciate it if you could help me with it: https://stackoverflow.com/q/62699111/13097721 Also, I'm very grateful and thankful for your multiple helps! – Upchanges Jul 03 '20 at 00:17
  • Hi @gelonida, I solved it by looking at this question: https://stackoverflow.com/questions/18290142/multiple-forms-in-a-single-page-using-flask-and-wtforms :) But, thank you! – Upchanges Jul 03 '20 at 07:35
  • Hi @gelonida, I messed up big times in my sqlite database and I would very appreciate it if you could help me: https://stackoverflow.com/q/62967601/13097721 . Thank you! – Upchanges Jul 18 '20 at 11:00
1

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 :)

Upchanges
  • 310
  • 2
  • 14