· 4 years ago · Dec 15, 2020, 08:44 PM
1#URL.PY#
2from django.conf.urls import url
3from django.contrib import admin
4from rest_framework.urlpatterns import format_suffix_patterns
5from webapp import views
6urlpatterns = [
7 url(r'admin/', admin.site.urls),
8 url(r'employees/', views.employeeList.as_views()),
9]
10VIEWS.PY
11from django.shortcuts import render
12
13# Create your views here.
14from django.http import HttpResponse
15from django.shortcuts import get_object_or_404
16from rest_framework.views import APIView
17from rest_framework.response import Response
18from rest_framework import status
19from .models import employees
20from .serializers import employeesSerializers
21
22
23class employeeList(APIView):
24 def get(self, request):
25 employees1 = employees.objects.all()
26 serializer =employeesSerializers(employees1, many=True)
27 return Response(serializer.data)
28
29 def post(self):
30 pass
31serializers#
32from rest_framework import serializers
33from .models import employees
34
35class employeesSerializers(serializers.ModelSerializers):
36
37 class Meta:
38 model = employees
39# fields = ('firstname','lastname')
40 fields='__all__'
41from django.contrib import admin
42from .models import employees
43
44admin.site.register(employees)
45
46# Register your models here.
47MODELS.PY#
48from django.db import models
49
50class employees(models.Model):
51 firstname= models.CharField(max_length=20)
52 lastname= models.CharField(max_length=20)
53 emp_id= models.IntegerField()
54
55 def __str__(self):
56 return self.firstname
57
58SETTINGS
59"""
60Django settings for myproject project.
61
62Generated by 'django-admin startproject' using Django 3.1.4.
63
64For more information on this file, see
65https://docs.djangoproject.com/en/3.1/topics/settings/
66
67For the full list of settings and their values, see
68https://docs.djangoproject.com/en/3.1/ref/settings/
69"""
70
71from pathlib import Path
72
73# Build paths inside the project like this: BASE_DIR / 'subdir'.
74BASE_DIR = Path(__file__).resolve().parent.parent
75
76
77# Quick-start development settings - unsuitable for production
78# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
79
80# SECURITY WARNING: keep the secret key used in production secret!
81SECRET_KEY = 'a-04u(4efpvypz#z_$r@x22_4wg@_-f88t=%t%k_t=69gudw#='
82
83# SECURITY WARNING: don't run with debug turned on in production!
84DEBUG = True
85
86ALLOWED_HOSTS = []
87
88
89# Application definition
90
91INSTALLED_APPS = [
92 'django.contrib.admin',
93 'django.contrib.auth',
94 'django.contrib.contenttypes',
95 'django.contrib.sessions',
96 'django.contrib.messages',
97 'django.contrib.staticfiles',
98 'rest_framework',
99 'webapp',
100]
101
102MIDDLEWARE = [
103 'django.middleware.security.SecurityMiddleware',
104 'django.contrib.sessions.middleware.SessionMiddleware',
105 'django.middleware.common.CommonMiddleware',
106 'django.middleware.csrf.CsrfViewMiddleware',
107 'django.contrib.auth.middleware.AuthenticationMiddleware',
108 'django.contrib.messages.middleware.MessageMiddleware',
109 'django.middleware.clickjacking.XFrameOptionsMiddleware',
110]
111
112ROOT_URLCONF = 'myproject.urls'
113
114TEMPLATES = [
115 {
116 'BACKEND': 'django.template.backends.django.DjangoTemplates',
117 'DIRS': [],
118 'APP_DIRS': True,
119 'OPTIONS': {
120 'context_processors': [
121 'django.template.context_processors.debug',
122 'django.template.context_processors.request',
123 'django.contrib.auth.context_processors.auth',
124 'django.contrib.messages.context_processors.messages',
125 ],
126 },
127 },
128]
129
130WSGI_APPLICATION = 'myproject.wsgi.application'
131
132
133# Database
134# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
135
136DATABASES = {
137 'default': {
138 'ENGINE': 'django.db.backends.sqlite3',
139 'NAME': BASE_DIR / 'db.sqlite3',
140 }
141}
142
143
144# Password validation
145# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
146
147AUTH_PASSWORD_VALIDATORS = [
148 {
149 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
150 },
151 {
152 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
153 },
154 {
155 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
156 },
157 {
158 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
159 },
160]
161
162
163# Internationalization
164# https://docs.djangoproject.com/en/3.1/topics/i18n/
165
166LANGUAGE_CODE = 'en-us'
167
168TIME_ZONE = 'UTC'
169
170USE_I18N = True
171
172USE_L10N = True
173
174USE_TZ = True
175
176
177# Static files (CSS, JavaScript, Images)
178# https://docs.djangoproject.com/en/3.1/howto/static-files/
179
180STATIC_URL = '/static/'
181