Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Last active March 24, 2022 08:07
Show Gist options
  • Save neilkuan/633af34d39ffa0afa07c660516c4d238 to your computer and use it in GitHub Desktop.
Save neilkuan/633af34d39ffa0afa07c660516c4d238 to your computer and use it in GitHub Desktop.
Django Getting Start
# create a project dir.
$ mkdir django-lab
$ cd django-lab

# Use pipenv install django
$ pipenv install django

# activate virtualenvs use `pipenv shell`
$  pipenv shell

# create django project on current dir.
$ django-admin startproject storefront .

# run server via `python manage.py runserver`
$ python manage.py runserver
# add new app into django project
$ python manage.py startapp playground
# edit playground/views.py

from django.shortcuts import render
from django.http import HttpResponse, QueryDict
# Create your views here.


def say_hello(request):
    # return string direct 
    # return  HttpResponse("Hello  world!!!")

    # return html template via render
    name = request.GET.get('name', '')
    if name != '':
       return render(request, 'hello.html', {'name': name})
    else:
       return render(request, 'hello.html')
# import views to playground/urls.py
from . import views
from django.urls import path


## URL Conf.
urlpatterns = [
    path('hello/', views.say_hello),
]
# add `playground/views` path into storefront/urls.py
"""storefront URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('playground/', include('playground.urls')),  # <--
]
@neilkuan
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment