Create subpages with content from Database ?

Hello together,

Since I don`t really now how to google for this, I wonder if you guys can help me with my problem :slight_smile:

I am using Django and I am really happy about this (really like it ! ), but now I want to realise this project:

I have a database, which has 3 columns (example below)
ID NAME Active
0 Red yes
1 Blue yes
2 Orange yes
3 Green no
4 White yes
5 Yellow yes

My domain is example.com and now I want to create automatically the subpages from the database, if the active column is yes:
example.com/Red
etc.

And If I add a new line to the database, I want this added of course too (or removed, if I remove one)

Can anyone tell me what is the best idea to handle this problem? Or maybe give me a hint, what to google to find a proper solution?

Okay, I guess I found a solution by myself with PhP.

Now I would use example.com?name=Red

And I just use a PhP Script which saves the variable $name and then SELECT it from the database.

It’s not clear to me what you’re asking for.

Django does not “pre-prepare” pages. It builds the pages dynamically from the data in the database.

So what I think you’re looking for is to have the system only display pages where Active = yes. That then raises the question of how do they select one of these pages? Are the options being presented through some type of menu? Are the links provided somewhere else?

This might be a case where you’re looking at your desired solution from a non-Django angle. It might be helpful if we dig past what you think your solution is and get to what the real objective is.

That’s fine for what it does, but what you describe by itself does nothing to limit the selection to those entries with Active = yes. Someone could just as easily enter name=Green.

Okay, let`s look at this from a non-Django Angle:
I want to add dynamically content to my database (as listed in Post 1)
And I want to see the content of the database if I go for example on example.com?name=RED.
Then It should show me a plain Page with ID: 0 Name: Red, but only if Active=Yes

I would just add another check in the PHP WHERE Name="$name" AND Active=“YES”, this should prevent this problem or ?

So, you’re expecting the person coming to this site to know that name=RED is valid and gives a page, but name=Red and name=Green does not?

Django can do this. But there’s a lot to learn about Django to get to that point. I always recommend when someone is first learning Django that they work through either (or both!) the official Django tutorial or the Django Girls Tutorial. I will caution you that Django has a fairly significant learning curve and there’s a lot that needs to be done to get started.

(By the way, what you’ve listed as your where clause in PHP is a massive security hole, and the first person wishing to cause you problems can delete your database. Read about SQL Injection.)

Sorry, No the Person doesn`t know this, I´ll just send them a link. They just click on it.

Gonna check the tutorials, thx :slight_smile:

And of course I would not write it like this in PHP, it`s just simplified, so its easier to understand :smiley:

Ok, so in that case I suggest you think in terms of setting it up such that the url is something like example.com/red. Django makes it very easy to use a url component as a variable for selecting an entry from a table. (You’ll see that as you’re working through the tutorial.)