Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Hello World in Django 2.2
Anurag Rana
Anurag Rana

Posted on

     

Hello World in Django 2.2

We have already covered how tostart with Django 1.9. In this article, we will see how to quickly get a simple page running using Django 2.2.

Steps:

Create a virtual environment using python3.

virtualenv -p /use/bin/python3.5 venv

Activate the virtual environment.

source venv/bin/activate

Now install the Django2.2 inside this virtual environment using pip.

pip install Django==2.2

Once Django has been installed, run thedjango-admin command to start a project.

Create your project.

$ django-admin startproject helloworld

Go inside the newly created project directory.

$ cd helloworld

You will see a file manage.py and a directory with the same name as of your project.

Create an app here. Every project consists of one or more apps. These are plug-able modules that can be reused in another project if written properly.

$ python manage.py startapp hw

Go inside hw directory. create a new file urls.py.

Add the below lines to this file and save it.

fromdjango.urlsimportpathfrom.importviewsapp_name="hw"urlpatterns=[path(r'',views.home,name='home'),]

Open views.py file and save the below code in it. This will create the first view function which will be called when hitting base project URL i.e. localhost:8000/ on the local server.

fromdjango.shortcutsimportrender# Create your views here.defhome(request):data=dict()returnrender(request,'hw/home.html',data)

Include the hw app's URLs in the main project URLs file.

Open helloworld/urls.py file and add below line in urlpatterns.

fromdjango.contribimportadminfromdjango.urlsimportpath,includeurlpatterns=[path('admin/',admin.site.urls),path(r'',include('hw.urls',namespace="hw")),]

Now finally add hw in installed apps in helloworld/settings.py file.

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

Now start the Django development server by running the command.

$ python manage.py runserver

This will run the python HTTP server on the localhost and 8000 port. If you want to run it on a different port, use port number in the command. For example$ python manage.py runserver 8888.

Use the below command to make your project available for everyone on the network.

$ python manage.py runserver 0.0.0.0:8000

Open the web browser and got to URL localhost:8000 and you will be able to see the home page.

Source code:

The source code is available onGitHub.

Host your Django Application for free onPythonAnyWhere. If you want full control of your application and server, you should considerDigitalOcean. Create an account with thislink and get $100 credits.

This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Backend Developer. Working on Python and Java. Writes about Python and Django on https://www.pythoncircle.com
  • Joined

More fromAnurag Rana

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp