Commit 8f765b90 authored by Georgios Xonikis's avatar Georgios Xonikis

Django Rest Framework

parent 3b4fd861
# Generated by Django 2.2.2 on 2019-06-12 15:24
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0003_auto_20190612_1112'),
]
operations = [
migrations.AlterField(
model_name='post',
name='created_at',
field=models.DateTimeField(default=datetime.datetime.now),
),
]
# Generated by Django 2.2.2 on 2019-06-12 15:35
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0004_auto_20190612_1524'),
]
operations = [
migrations.AlterField(
model_name='post',
name='created_at',
field=models.DateTimeField(auto_now_add=True),
),
]
......@@ -13,4 +13,4 @@ class Post(models.Model):
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
\ No newline at end of file
return self.title
from django.contrib import admin
from django.urls import path
from blog.views import view_post_details
urlpatterns = [
path('', view_post_details), # name='get-all-posts'
#path('<int:post_id>/', GetPostView.as_view(), name='get-post')
]
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.views import View
# import the table from the Model
from .models import Post
# Create your views here.
def view_post_details(response):
# Store the field in a variable post from a Django query
# type(post) = <class 'django.db.models.query.QuerySet'>
post = Post.objects.values('author', 'title', 'body', 'created_at').all()
# to view all fields
# post = Post.objects.values().all()
# convert it to a list
post = list(post)
return JsonResponse(post, safe=False)
......@@ -132,7 +132,8 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
TIME_ZONE = 'CET'
#TIME_ZONE = 'UTC'
USE_I18N = True
......@@ -141,6 +142,7 @@ USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
......
......@@ -18,6 +18,9 @@ from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
#path('api-auth/', include('rest_framework.urls')),
#path('blog/', include('app.feed.urls')),
path('blog/', include('blog.urls')),
# Django REST
# path('api-auth/', include('rest_framework.urls')),
]
......@@ -156,7 +156,6 @@ INSTALLED_APPS = [
]
13. Create a model at <app name> / models -->
example:
class Post(models.Model):
......@@ -178,17 +177,33 @@ python manage.py makemigrations
python manage.py migrate
15. Check that the table has created -->
15. Check that the table has been created -->
table name = <app name>_<model class name in lowercase>
ex: blog_post
16. Add it to the admin
16. If you want to give access to the admin,
Go to app directory / admin.py -->
admin.site.register(<model class name>)
ex: admin.site.register(Post)
17. Create the views inside the app / views.py
18. Create a urls.py inside the app directory to include the app's views -->
<app name> / urls.py
urlpatterns = [
path('', view_all), # name='get-all-posts'
]
19. Include in django's urls.py the path of the app's urls -->
<django project > / urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
#################################################################
Create Django REST API
......@@ -226,5 +241,5 @@ REST_FRAMEWORK = {
}
5.
5. HOW TO CHANGE THE TIMEZONE????????
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment