dynamically change pages with slug and include tag in template

Hello,

I was trying to build a list of website template preview in my website, which is when user click the specific webpage template preview button on the templates list page, it will direct to the webpage template detail page,the urls.py code is like below:

urls.py:

urlpatterns = [path('choose_templates/<slug:slug>/', views.template_preview, name='template_preview'),]

views.py:

```def template_preview(request,slug):
    context = {
        'slug':slug
    }
    return render(request,'./html/htmlbase.html',context)```

htmlbase.html:

```<!DOCTYPE html>
<html>
  <head></head>
  <body>
  {% include slug %}
</body>
</html>```

what I want to do is when user click on different template preview button, it will direct to the correspond template detail page. the code above result in " TemplateDoesNotExist".

Could anyone helo me with this?

Thank you!

First, when you’re posting code here, please enclose it between two lines consisting only of three backtick (`) characters. This means you’ll have one line of only ```, followed by your lines of code, followed by another line of ```. This keeps your formatting and prevents special characters from being replaced. (Make sure you use the backtick - ` and not the apostrophe - '. Also, you don’t need to add a reply to do this. You can edit your original post and put the lines of ``` before and after each block of code.)

And when you talk about a “template preview” page - what do you mean? Are you referring to what Django means as a “template”, or are you referring to something else? (I’m trying to understand exactly what your intent is here.)

Ken

Thank you for your advise, I am new to django forum, I have revised my question, looks better now.

the “template preview” means webpages template, not the term in django template. I am trying to make a website which include few webpage template for users to choose, and they can click on the “preview” button to see how’s the webpages look like.

So specifically, within the context of this system, what is a “template”? Is it a file, is it data within a table, or is it something else?

(Also, given how frequently the word “template” is used within django for its own purposes, I strongly recommend you find another name to call whatever-this-is. It’s going to avoid a lot of confusion later on.)

Ken

Yea… the template I mentioned was a html file actually.

Ok, so somewhere you’re going to want to associate the slug with an actual file name.

If it’s just a pure static html fragment (without the <html> or <body> tags - nothing of higher structure than a <div>) then you can use the include tag, if the fragment is in the template directory and your slug is associated with the right file.

Or, if you want those samples external to your templates, you could just read the file in your view and add it to the context of the template being rendered.

yes, the result I want was like different route to different html file, like below:
route to demo1 html file:
http://127.0.0.1:8000/choose_templates/demo1/

route to demo2 html file:
http://127.0.0.1:8000/choose_templates/demo2/

since I can only pass single ‘template’ to ‘render’, so my thought was use a ‘base.html’ in ‘render’ method, and use ‘{% include %}’ tag in ‘template’ to add different html page to the ‘base.html’ with the provided ‘slug’, but I don’t know how exactly to do that…

I hope you are able to understand.

Yes, I get that now. You’re on the right track - but you need something (e.g. data in a model) that associates “demo1” with “template_app/demo1.html”. The name “demo1” itself is not the name of a template file. You need to supply the location of the template file in your include tag variable.

In other words, if the slug is “demo1” referring to a template file located in “samples/demo1_sample.html”, then your include tag needs to evaluate to:
{% include "samples/demo1_sample.html" %}

So, your view might do something like:
sample_file_name = "samples/%s_sample.html" % slug
context = { 'sample_file': sample_file_name}

Then your template could do:
{% include sample_file %}

Wow! loved it! that’s super awesome! it works! I am so excited!

THANK YOU SO MUCH!

By the way, I was trying to use ‘f string’ to solve the issue, but failed. maybe I did it wrong, Do you think ‘f string’ could also solve the problem?

Yes, you’d be able to do this with an f-string.