Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Apr 21, 2021. It is now read-only.

Archived in favor ofhttps://github.com/jayhale/vercel-django-example

NotificationsYou must be signed in to change notification settings

jayhale/now-django-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tutorial

Install Django

$ mkdir now-django-example$ cd now-django-example$ pip install Django$ django-admin startproject now_app .

Add an app

$ python manage.py startapp example

Add the new app to your application settings (now_app/settings.py):

# settings.pyINSTALLED_APPS= [# ...'example',]

Be sure to also include your new app URLs in your project URLs file (now_app/urls.py):

# now_app/urls.pyfromdjango.urlsimportpath,includeurlpatterns= [    ...path('',include('example.urls')),]

Create the first view

Add the code below (a simple view that returns the current time) toexample/views.py:

# example/views.pyfromdatetimeimportdatetimefromdjango.httpimportHttpResponsedefindex(request):now=datetime.now()html=f'''    <html>        <body>            <h1>Hello from Zeit Now!</h1>            <p>The current time is{now}.</p>        </body>    </html>    '''returnHttpResponse(html)

Add the first URL

Add the code below to a new fileexample/urls.py:

# example/urls.pyfromdjango.urlsimportpathfromexample.viewsimportindexurlpatterns= [path('',index),]

Test your progress

Start a test server and navigate tolocalhost:8000, you should see the index view you justcreated:

$ python manage.py runserver

Get ready for Now

Add the Now configuration file

Create a new filenow.json and add the code below to it:

{"version":2,"name":"now-django-example","builds": [{"src":"now_app/wsgi.py","use":"@ardnt/now-python-wsgi","config": {"maxLambdaSize":"15mb" }    }],"routes": [        {"src":"/(.*)","dest":"now_app/wsgi.py"        }    ]}

This configuration sets up a few things:

  1. "src": "now_app/wsgi.py" tells Now thatwsgi.py contains a WSGI application
  2. "use": "@ardnt/now-python-wsgi" tells Now to use thenow-python-wsgi builder (you canread more about the builder athttps://github.com/ardnt/now-python-wsgi)
  3. "config": { "maxLambdaSize": "15mb" } ups the limit on the size of the code blob passed tolambda (Django is pretty beefy)
  4. "routes": [ ... ] tells Now to redirect all requests ("src": "/(.*)") to our WSGIapplication ("dest": "now_app/wsgi.py")

Add Django to requirements.txt

Thenow-python-wsgi builder will look for arequirements.txt file and willinstall any dependencies found there, so we need to add one to the project:

# requirements.txtDjango==2.2.4

Update your Django settings

First, update allowed hosts insettings.py to include.now.sh:

# settings.pyALLOWED_HOSTS= ['.now.sh']

Second, get rid of your database configuration since many of the libraries django may attempt toload are not available on lambda (and will create an error when python can't find the missingmodule):

# settings.pyDATABASES= {}

Deploy

With now installed you can deploy your new application:

$ now> Deploying now-django-example under jayhale...> Success! Deployment ready [57s]

Check your results by visitinghttps://zeit.co/dashboard/project/now-django-example


[8]ページ先頭

©2009-2025 Movatter.jp