-3

Hi I'm trying to build a simple django app but I can't get index.html file to recognise the variables created in views.py. Nothing I do seems to work. Does it have to be configured somehow in settings.py perhaps?

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>Hello world {{ name }}</p>
</body>
</html>

views.py:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def index(request):
    context = {'name' : 'Peter'}
    return render(request, 'index.html', context)

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index')
]

Whatever I try, the browser just doesn't recognise anything in the {{}}. It just leaves it blank. I seem to have tried everything. Please help, I'm tearing my hair out.

Here's the github repo if anyone cares to look at it:

https://github.com/Peterhague/stacqato2

Thanks

Peter82
  • 1
  • 1
  • Your code looks fine and `{{ name }}` should work in the template. Is this exactly the code you are running, or have you omitted/simplified it before posting it here? – solarissmoke Apr 29 '22 at 07:22
  • No it's exactly like that – Peter82 Apr 29 '22 at 07:31
  • check if you have 'django.template.context_processors.request', in context_processors of templates – Pavan Kumar T S Apr 29 '22 at 07:37
  • Yes I've got that. I really haven't changed the default django files much at all. – Peter82 Apr 29 '22 at 07:39
  • Your github repo appears to be private - the link 404's – solarissmoke Apr 29 '22 at 07:43
  • GitHub link is broken – Pavan Kumar T S Apr 29 '22 at 07:43
  • sorry, it's public now – Peter82 Apr 29 '22 at 07:47
  • Your code runs perfectly fine for me. The issue is that you have committed files in `__pycache__` directories, which should never be committed. Remove all the `__pycache__` directories from your repository, and then restart your Django server, and it should work. Also fix the template name (the answer posted below is wrong). – solarissmoke Apr 29 '22 at 07:52
  • See https://stackoverflow.com/questions/3719243/best-practices-for-adding-gitignore-file-for-python-projects for how to ignore those directories in git. – solarissmoke Apr 29 '22 at 07:55
  • Now I'm getting a template does not exist error. All I did was go to the pycache files on the repo and selected delete directory. No other changes. – Peter82 Apr 29 '22 at 08:02
  • You are getting `TemplateDoesNotExist` because you changed `index.html` to `/index.html`, an incorrect suggestion made in one of the answers. – solarissmoke Apr 29 '22 at 08:40
  • It seems to be working now... sort of. couple more issues: 1. I seem to have to install django every time I open the repo. No idea why. 2. I can't seem to exit the server once it's running. Says to press ctrl+c but does nothing for me 3. the template still doesn't seem to be updating for changes to the variables (that's why I want to exit the server btw). Regular html entered into index.html updates, but not changes to the dictionary passed via views.py. Any ideas? Thanks to everyone by the way, much appreciated. – Peter82 Apr 29 '22 at 09:42

3 Answers3

-1

I think you're missing a slash before index.html in your render statement. Try this:

return render(request, '/index.html', context)

Fikran
  • 5
  • 3
-1

In your 'stacqato/settings.py' file I see that you have not added your app to the list of installed apps. You should add 'stack.apps.StackConfig' to INSTALLED_APPS in 'stacqato/settings.py'

In your settings.py file, in the TEMPLATES-section, APP_DIRS is true, so you should create a directory 'stack' under your 'templates' directory and move the 'index.html' file to the 'templates/stack' directory.

I mostly use class based views, so in your views.py that would become something like:

class IndexView(generic.View):
    template_name = 'stack/index.html'
    context = {'name': 'Peter'}

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name, context)

In your urls.py that translates to:

path('', views.IndexView.as_view(), name='index'),
herriejr
  • 27
  • 2
-1

I took a look at your Github repo. You didn't mention stack app in installed apps

enes islam
  • 1,054
  • 4
  • 12