简体   繁体   中英

Trying to create my first Django webpage but nothing loads when I run manage.py

I am following this tutorial: https://realpython.com/get-started-with-django-1/ .

I followed everything exactly the same but when I run manage.py, localhost:8000 only displays a blank webpage. I have a very simple HTML file that should display "Hello, World." but it's not loading.

Here is the code in settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world',

views.py:

from django.shortcuts import render


# Create your views here.
def hello_world(request):
    return render(request, 'hello_world.html', {})

My hello_world.html file:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <h1>Hello, World!</h1>

</head>
<body>

</body>
</html>

The code in my project "personal_portfolio" personal_portfolio\urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('hello_world.urls')),
]

And finally hello_world\urls.py:

from django.urls import path
from hello_world import views

urlpatterns = {
    path('', views.hello_world, name = 'hello_world'),
}

I've made sure all the files are in the right place, but I can't figure out why localhost:8000 only displays a white page. When I run the hello_world.html file locally, it works just fine. Like this: local HTML file

Any tips would be much appreciated. I'm frustrated I can't get this to work.

Your app url patterns are defined as a dictionary and should be a list.

So in hello_world/urls.py , change this;

urlpatterns = {
    path('', views.hello_world, name = 'hello_world'),
}

To be a list of paths;

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

You also have your <h1> tag in the head, not the body. You need to use a <title> tag in the head & move the <h1> ;

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
 
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM