Update django.md

main
Matt Huntington 5 years ago committed by GitHub
parent cafd7a07a7
commit a125b1c495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,3 +1,7 @@
# Intro to Django
## Setup
``` ```
python3 -m venv tutorial-env python3 -m venv tutorial-env
source tutorial-env/bin/activate source tutorial-env/bin/activate
@ -22,6 +26,8 @@ INSTALLED_APPS = [
] ]
```` ````
## Create a model
add to contacts_api/models.py add to contacts_api/models.py
```python ```python
@ -77,31 +83,7 @@ python manage.py runserver
go to http://localhost:8000/admin/ go to http://localhost:8000/admin/
in django_rest_api/urls.py edit ## Create api endpoints
```python
from django.contrib import admin
from django.urls import path
from django.conf.urls import include # add this
urlpatterns = [
path('', include('contacts_api.urls')), # add this
path('admin/', admin.site.urls),
]
```
create contacts_api/urls.py and add
```python
from django.urls import path
from . import views
from rest_framework.routers import DefaultRouter
urlpatterns = [
path('api/contacts', views.ContactList.as_view(), name='contact_list'),
path('api/contacts/<int:pk>', views.ContactDetail.as_view(), name='contact_detail'),
]
```
install djangorestframework: install djangorestframework:
@ -136,6 +118,32 @@ class ContactSerializer(serializers.HyperlinkedModelSerializer):
fields = ('id', 'name', 'age',) fields = ('id', 'name', 'age',)
``` ```
in django_rest_api/urls.py edit
```python
from django.contrib import admin
from django.urls import path
from django.conf.urls import include # add this
urlpatterns = [
path('', include('contacts_api.urls')), # add this
path('admin/', admin.site.urls),
]
```
create contacts_api/urls.py and add
```python
from django.urls import path
from . import views
from rest_framework.routers import DefaultRouter
urlpatterns = [
path('api/contacts', views.ContactList.as_view(), name='contact_list'),
path('api/contacts/<int:pk>', views.ContactDetail.as_view(), name='contact_detail'),
]
```
set contacts_api/views.py to set contacts_api/views.py to
```python ```python
@ -151,3 +159,7 @@ class ContactDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Contact.objects.all() queryset = Contact.objects.all()
serializer_class = ContactSerializer serializer_class = ContactSerializer
``` ```
## Add CORS
## Switch to Postgres
## Deploy to Heroku

Loading…
Cancel
Save