Skip to content

Instantly share code, notes, and snippets.

View anshuUnity's full-sized avatar
🎯
Learning

Anshu Pal anshuUnity

🎯
Learning
View GitHub Profile
@anshuUnity
anshuUnity / views.py
Last active June 24, 2021 12:31
django-csv
import csv
from django.http import HttpResponse
from django.contrib.auth.models import User
def user_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="users.csv"'
csv_writer = csv.writer(response)
from your_app import views
from django.urls import path
urlpatterns = [
...
path('csv/', views.user_csv, name='users_csv'),
]
...............
<a href="{% url 'users_csv' %}">Export CSV</a>
...............
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
from django.core.management.base import BaseCommand
from django.utils import timezone
class Mycommand(BaseCommand):
help = 'Test if custom command is working or not'
def handle(self, *args, **kwargs):
self.stdout.write("Hi all set, it is working.")
@anshuUnity
anshuUnity / m2m.py
Created July 26, 2021 17:44
m2m test
@receiver(m2m_changed, sender=Order.order_name.through)
def checkquan(sender, instance, **kwargs):
print("Working")
prod = instance.order_name.all()
for p in prod:
p.quantity -= instance.order_quantity
p.save()
class Upload(models.Model):
title = models.CharField(max_length=50)
image = models.ImageField()
def __str__(self):
return self.title
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
from django import forms
from upload.models import Upload
class UploadForm(forms.ModelForm):
class Meta:
model = Upload
fields = ['title', 'image']
from django.shortcuts import render
from django.views.generic import View
from upload.forms import UploadForm
from django.http import JsonResponse
# Create your views here.
class HomeView(View):
def get(self, request):
form = UploadForm()