DEV Community

loading...

DJANGO PAGES.... how do you have a home.html as a template? ITS NOT WORKING

Bret
・1 min read

i tried to render a html file, but it didn't work,
what do the urls.py path look like?

i followed theNetNinja but it didn't work

Discussion (7)

Collapse
qosha1 profile image
Quinn

Is this what you're talking about? For my site I just added templateViews as views. This puts the basic template at topmarq.com/about/ and uses the 'about.html' template. You can do the same with your home template.

urlpatterns = [
  ...
  url(
    r"^about/$",
    TemplateView.as_view(template_name="pages/about.html"),
    name="about",
),
...
]
Collapse
yobretyo profile image
Bret Author

Yes, how many different ways is there to doing that? It dosnt look like how several tutorials did it.

Collapse
qosha1 profile image
Quinn

Hmm can you show the tutorial?

So this method assumes that you don't have a dedicated view function for this HTML file. This only makes sense for pages where no context is needed. Eg 'static' pages. If your 'home' page isn't static you'll need to create a view function, and then call that function.

For a page that is not static, I call it this way:

urlpatterns = [
    # Main Views
    url(regex=r"^popular$", view=views.PopularThreadListView.as_view(), 
name="thread_popular"),

And then in the App, I have a views.py that has a 'view' like this:
class PopularThreadListView(ListView):
"""A basic ListView """

model = Thread
paginate_by = 5
context_object_name = "threads"

def get_context_data(self, *args, **kwargs):
    context = super().get_context_data(*args, **kwargs)
    return context

# Return the top most viewed recent Threads.
def get_queryset(self, **kwargs):
    return Thread.objects.get_popular()
Thread Thread
yobretyo profile image
Bret Author

I’ve been watching “theNetNinja” Django tutorial. When do I make a migration?

Thread Thread
qosha1 profile image
Quinn

Any time you change the structure of your database you will need to 'makemigrations' (creates a file that tells the DB what to change and how) and then 'migrate' (actually tells the DB to update).
For instance, if you change your 'User' model to now include their birthday as a datetime field, you would need to go through this process to add a column in the user Table to store the birthdays.

Thread Thread
yobretyo profile image
Bret Author

Ok, do I CD into anything or:
./venv/bin/activate
makemigrations

?

Thread Thread
qosha1 profile image
Quinn • Edited

just be in the directory that contains your manage.py file.

Then run

python manage.py makemigrations
python manage.py migrate