Day 086 - Project 86 : Blog Time

If you have any questions, comments, or issues with this project please post them here!

Hi Guys,

I am so stuck with this… and even with David’s Solution I cannot figure out, why my blog entries are not showing…

Please, please have a go at my code (you have to set secret key and password first, and some entries).

The db storing works fine but something in my code is messed up, that it won’t view the blog entries. :sob:

Please help :cry:

This is my repl:

https://replit.com/@surfineer/Day-86-BLOG-WEBSITE?v=1

THANKS SO MUCH FOR ANY ADVICE :pray:

As you put in your code

try: #I think this part is not working correctly
        new = new.replace("{date}",db[key])
        new = new.replace("{title}",db[key]["title"])
        new = new.replace("{blogtext}",db[key]["blogtext"])
        content += new

it appears that you’re trying to replace {date} with the entire value associated with key. However, according to your blog_entry() function, db[key] (where key is date) is a dictionary with title and blogtext as keys.

I think you probably meant to replace {date} with the key itself.

Try updating your view() to something like:

def view():
  keys = list(db.keys())
  page= ""
  f = open("templates/content.html","r")
  page = f.read()
  content = ""
  for key in reversed(keys):
    if key != "password":
      new = page
      try: 
        new = new.replace("{date}", key)  # Replace with 'key', not 'db[key]'
        new = new.replace("{title}", db[key]["title"])
        new = new.replace("{blogtext}", db[key]["blogtext"])
        content += new
      except:
        print("it's this")
  return content

2 Likes

Yes!!! That was it! :partying_face: Thank You so much!! :pray: :blush:

No problem! Don’t forget to mark the answer as solved as it can help other people!

I think only admins can do that, as I cannot find a button for it…

Nope. You can mark topics you created as solved and TL4+ can mark other people’s topics. The reason you can’t is because the Solved plugin was disabled for #code-help:100-days-of-code.

1 Like

hi there,

On day 86 of 100 there task is to create a blog that allows 1 user (one’s self) to log in and then add and view their own posts.

I have added a sign up function which, given I am using the replit DB, mixes the key / values of users.

later I am trying to go to the DB and get all the posts that have a key-value pair “date”:date and this try block does not get executed and goes to the exception which seems to inform me that the key is the issue.

I am aware this is not great code and there are other issues. I am trying to solve this first but would love more feedback overall. Thank you in advance.

To better diagnose the problem, remove try and except (but leave the code you want to try). This way, the program will crash with an error message. You can use this error message to better diagnose your problem.

1 Like

I would add some prints in the try block to understand when the except is triggered (before the if, before the for, before the return)

1 Like

Hi all… I am wondering… added one entry for few days back… one for today… works great… added one more for today, it overwrites it… and it gets weirder still… added for two days ahead, it displays it before the oldest post… any thoughts?
here’s a link to my repl… thank you all in advance, would be of great help if I could understand why is it acting this way…
https://replit.com/@Thrsh001/86100-blog?v=1

1 Like

Has any of you got this message over and over again?

The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

Ive set a secret key and place it correctly in the app.secret_key= os.environ['bbeewww']

Everything works fine but when I try to assign a session to my user–> Internal server error due to this.

Any idea?

If you want to have a look:
https://replit.com/@python-padawan/Day86100Days#main.py

Have you created a secret called “bbeewww”?

1 Like

yes.
this was my 3rd attempt of creating a secret as I have continuously got that error.

I think it might be cause you’re redefining app and not adding the secret key again…

What do you mean? I don’t get it
I create a secret every time key+value

I mean you define app with app = Flask(__name__), then you set the secret_key of that app. Then you redefine app with another app = Flask(__name__). But you never set the secret_key for that app.

So here’s your current code:

app = Flask(__name__) # Create app
app.secret_key = os.environ['bbeewww'] # set app's secret_key to the secret key you created

app = Flask(__name__) # Redefine app so the secret_key you set for the other app won't carry over

Bro, the second app = Flask(__name__) was duplicated.

I guessed it slided there from so much copy and paste from previous exercises.

It started working as soon as I erased it.

Thanks!

Question: I completed the day 86 challenge to build a blog with help from the provided solution and i thought i would add a delete button. The key to my blogs is the date, which is the same as the solution, however when I try to delete a blog entry it only deletes the title and body text, and leaves the entry with only the date remaining?
How can i delete the entire blog entry, date and all?

Repl link:

This is the code for the delete route which the button activates:

@app.route("/delete")
def delete():
  form = request.form
  date = form["date"]
  if date in db:
    del db[date]
  return

@JaredGodfrey can you please send the link to your Repl?