Difference between revisions of "Simple "Hello world" application"

From MyWiki
Jump to: navigation, search
 
(17 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
''$ cd lecture3<br>''
 
''$ cd lecture3<br>''
 
'''Run the webserver then navigate to http://localhost:8000 to check that it is working'''<br>
 
'''Run the webserver then navigate to http://localhost:8000 to check that it is working'''<br>
 +
''$ python manage.py runserver'' <br>
 +
'''Create a simple app called "hello"''' <br>
 +
''$ python manage.py startapp hello'' <br>
 +
This will create a folder called "hello" containing a variety of files<br>
 +
'''In the lecture3 folder edit the settings.py file and add the hello app to the configuration'''
 +
<source lang="python">
 +
INSTALLED_APPS = [
 +
    'hello',
 +
    'django.contrib.admin'
 +
</source>
 +
'''In the hello folder edit the views.py file''' <br>
 +
At the top of the file add "from django.http import HttpResponse" <br>
 +
Then add
 +
<source lang="python">
 +
def index(request):
 +
      return HttpResponse("Hello World")
 +
</source>
 +
'''Again in the "hello" folder create a urls.py file with the following contents:'''
 +
<source lang="python">
 +
 +
    from django.urls import path
 +
    from . import views
 +
    urlpatterns={
 +
        path("", views.index, name="index")
 +
    }
 +
</source>
 +
'''In the lecture3 directory edit the urls.py file and add "include" to the import line, then add:'''
 +
<source lang="python">
 +
path('hello/', include("hello.urls"))
 +
</source>
 +
so that the urls.py file in the lecture3 directory now looks like:
 +
<source lang="python">
 +
from django.contrib import admin
 +
from django.urls import path, include
 +
 +
urlpatterns = [
 +
    path('admin/', admin.site.urls),
 +
    path('hello/', include("hello.urls"))
 +
]
 +
</source>

Latest revision as of 14:32, 18 March 2021

Start a project:
$ django-admin startproject lecture3
Change directory into the folder "lecture3:
$ cd lecture3
Run the webserver then navigate to http://localhost:8000 to check that it is working
$ python manage.py runserver
Create a simple app called "hello"
$ python manage.py startapp hello
This will create a folder called "hello" containing a variety of files
In the lecture3 folder edit the settings.py file and add the hello app to the configuration

INSTALLED_APPS = [
    'hello',
    'django.contrib.admin'

In the hello folder edit the views.py file
At the top of the file add "from django.http import HttpResponse"
Then add

 def index(request):
       return HttpResponse("Hello World")

Again in the "hello" folder create a urls.py file with the following contents:

    from django.urls import path
    from . import views
    urlpatterns={
        path("", views.index, name="index")
    }

In the lecture3 directory edit the urls.py file and add "include" to the import line, then add:

path('hello/', include("hello.urls"))

so that the urls.py file in the lecture3 directory now looks like:

from django.contrib import admin
from django.urls import path, include
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', include("hello.urls"))
]