Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
https://blog.sblip.dev

SblipDev shaurya-blip

💻
https://blog.sblip.dev
View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am shaurya-blip on github.
  • I am sblipdev (https://keybase.io/sblipdev) on keybase.
  • I have a public key ASChL-n6owvUHN6DHVTnBiyfkQDOkUStcFKnNUZ51tWm2Ao

To claim this, I am signing this object:

View predict.py
# Coefficients
print('Coefficients: \n', regressor.coef_)
# The mean squared error
print("Mean squared error: %.2f" % np.mean((regressor.predict(X_test) - y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regressor.score(X_test, y_test))
View settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'sample', # DATABASE NAME
'USER': 'postgres', # NAME OF USER (DEFAULT IS postgres)
'PASSWORD': 'xyz', # whatever password you have kept.
'HOST': '127.0.0.1', # HOST in which the psql server is running on
'PORT': '5432', # PORT in which the psql server is running on
}
}
View request.py
import requests
def create_book(title, author):
headers={
'title':title,
'author':author
}
response = requests.post('http://127.0.0.1:8000/books/', headers)
print(f"New book {title} by {author} has been created,\n\n")
View urls.py
from django.urls import include, path
from rest_framework import routers
from .views import BookViewSet
router = routers.DefaultRouter()
router.register(r'books', BookViewSet)
urlpatterns = [
path('', include(router.urls)),
View urls.py
"""Books URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.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
View views.py
from rest_framework import viewsets
from .serializer import BookModelSerializer
from main.models import Book
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookModelSerializer
View serializer.py
from rest_framework import serializers
from main.models import Book
class BookModelSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
View settings.py
INSTALLED_APPS = [
'main',
...
'rest_framework'
]
View admin.py
from django.contrib import admin
from .models import Book
# Register your models here.
admin.site.register(Book)