Include a URL from another page

So I have this model for a page

from django.db import models

class Page(models.Model):
    Title = models.CharField(max_length=128)
    URL = models.CharField(max_length=128)
    Content =  models.TextField()

In the urls.py file of the app I want to replace the path(‘this part’) with the URL from the model so when I create a new page and give it a URL that the URL is then inserted into the path.

How do I get access to variables from models?

It’s not done directly from within the urls. Typically, you identify a variable / placeholder within your URL, and then your view accepts that variable and maps that to your model instance.

This example from the django tutorial shows how this can be done and should give you some ideas.

Hi Ken hope all is well

What I want to do is specify a URL in a field on the page content type (model). Then replace that URL inside the urls.py file for the pages app. Like Drupal when you create a new page you give it a URL and when you save and go to that URL you see the page. So in admin I have pages and if I click on a page I can enter a title, a url and the page content. So when I save that page let’s say about page and I go to localhost:8000/about I want to see the page. So dynamically create pages like in Wordpress and Drupal but using Django, without the need to specify a required site. Cause I want to do this on my local computer and not on a real domain. Appreciate the help

Hello Calvin, fancy meeting you here! :grinning:

Ok, this is another one of those “mind-shift” situations. You don’t want to dynamically change your urls, you want your view to accept a url variable as a parameter, then retrieve the page based on that variable.

Working under the assumption that you’re still working with the flatpages app, take a look at the Using the URLconf documentation, particularly the section on the “catchall” pattern.

I think this may be what you’re looking for - however, if this question isn’t in the context of flatpages, holler.

Ken

Hi Ken

I kinda got stuck with the flatpages app because of the required sites included module. I basically just want to create a page, give it a url, save it and then go to that url to see the new page I created. So many questions, so little time :smile:

Oh that’s fine - the flatpages app is there to be used. I just wanted to ensure that the answer I provided was the right answer for the question being asked. (If you weren’t using flatpages, my answer would have been significantly different - but still along the same idea that you wouldn’t be dynamically changing your urls file.)

Ok thanks but I really want to like try and make my own version and not be forced to use flatpages. It forces me to enter a site and makes it too difficult to use on my local pc, hence the reason for trying my own solution. I think I just don’t know enough yet to make this work. I will study more and try and find out more.

Oh, ok. The idea is similar, there’s just a little more work needed on your end.

You could have something in your urls (either root or in the context of an app) like:

path('<str:url>/', views.page_view),

Then your view could be something like this:

def page_view(request, url):
    page_text = Page.objects.get(URL=url)
    return render(request, "my page template", context={'page': page_text})

Then in your template you can use (( page.Title }} and {{ page.Content }} to populate your template.

(Note: This is just a skeleton to point you in the direction of a full solution. There’s no error_checking here for ensuring that URL is a proper value, etc.)

Hi Ken thank you

This is where I struggle. Here is my page model:

from django.db import models

class Page(models.Model):
    Title = models.CharField(max_length=128)
    URL = models.SlugField(max_length=128, unique=True, default=Title)
    Content =  models.TextField()

here is my urls.py (pages app) file

from django.urls import path
from .import views

urlpatterns = [
    path('<str:url>/', views.page_view),
]

and finally here is my views.py file

from django.shortcuts import render
from .models import Page

# Create your views here.
def page_view(request, url):
    page = Page.objects.get(url=URL)
    context = {
        "title" : page.Title,
        "content" : page.Content,
        }
    return render(request, "page.html", context)

I get an error:
name ‘URL’ is not defined

Secondly how does this dynamic url function pls <str:url>/

Hi Ken

Your advice put me on the right path thank you so much. I got the app to work and I am able to create new pages in admin, go to that URL and then see the page.

Here is my pages.urls file:

from django.urls import path
from .import views

urlpatterns = [
    path('<str:url>/', views.page_view),
]

and here is my pages.models file:

from django.db import models

class Page(models.Model):
    title = models.CharField(max_length=128)
    url = models.SlugField(max_length=128, unique=True, default=title)
    content =  models.TextField()
    published = models.BooleanField(default=True)

and then my pages.views file:

from django.shortcuts import render, get_object_or_404
from .models import Page

def page_view(request, *args, url):
    page = get_object_or_404(Page, url=url)
    context = {
        "title" : page.title,
        "content" : page.content,
        }
    return render(request, "page.html", context)

Could you explain how the dynamic url works pls <str:url>/

Another question pls: How do I make the url default whatever I enter in the title field?

Again you’ve been a great help I really appreciate it. I feel more confident.

More questions to come I am sure

Url default to title field:

There are actually three (or four) different ways to answer this that in part, depend upon how you’re entering data into your Page model and when/how you want this default to apply.

Briefly -

  1. You’re using the built-in Django admin and entering in your pages there. In this case, you would want to add a save_model method to your ModelAdmin class. (If you’re using an implicit method to create a ModelAdmin object, you’ll need to change that to explicitly create one for you to add this method.)

  2. You’ve got a form view you’ve built to accept entry for this model. You’ll need to assign this field in your view where the model is being saved.

  3. and 3a. You could not save it directly, but instead catch the exception when you do the search on the url field, and search a second time on the title - or, make a more complex query that searches for the url being in the url field or the title field if the url field is blank.

If you do any of these, you’ll probably also want to take a look at the slugify function.

Ken

For a better understanding of how Django handles URLs, see the URL dispatcher page.

Thank you so much for all the help. One day when I complete my AWESOME WORLD’S BEST MONEY MAKING APP I know who I’ve got to thank :laughing: