├── .gitattributes ├── LICENSE ├── README.md ├── bank ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-39.pyc │ ├── admin.cpython-39.pyc │ ├── apps.cpython-39.pyc │ ├── forms.cpython-39.pyc │ ├── models.cpython-39.pyc │ └── views.cpython-39.pyc ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_alter_customer_id.py │ ├── 0003_auto_20210531_2054.py │ ├── __init__.py │ └── __pycache__ │ │ ├── 0001_initial.cpython-39.pyc │ │ ├── 0002_alter_customer_id.cpython-39.pyc │ │ ├── 0002_auto_20210518_1233.cpython-39.pyc │ │ ├── 0003_auto_20210531_2054.cpython-39.pyc │ │ └── __init__.cpython-39.pyc ├── models.py └── views.py ├── core ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-39.pyc │ ├── settings.cpython-39.pyc │ ├── urls.cpython-39.pyc │ └── wsgi.cpython-39.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py ├── db.sqlite3 ├── manage.py ├── requirements.txt ├── starting_app.zip └── templates └── admin ├── bank └── customer │ └── change_list.html └── csv_upload.html /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 veryacademy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YT_Django_Admin_csv_Button_Upload 2 | 3 | -------------------------------------------------------------------------------- /bank/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__init__.py -------------------------------------------------------------------------------- /bank/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /bank/__pycache__/admin.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__pycache__/admin.cpython-39.pyc -------------------------------------------------------------------------------- /bank/__pycache__/apps.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__pycache__/apps.cpython-39.pyc -------------------------------------------------------------------------------- /bank/__pycache__/forms.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__pycache__/forms.cpython-39.pyc -------------------------------------------------------------------------------- /bank/__pycache__/models.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__pycache__/models.cpython-39.pyc -------------------------------------------------------------------------------- /bank/__pycache__/views.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/__pycache__/views.cpython-39.pyc -------------------------------------------------------------------------------- /bank/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path 3 | from django.shortcuts import render 4 | from .models import customer 5 | from django import forms 6 | from .models import customer 7 | from django.contrib import messages 8 | from django.http import HttpResponseRedirect 9 | from django.urls import reverse 10 | 11 | class CsvImportForm(forms.Form): 12 | csv_upload = forms.FileField() 13 | 14 | class CustomerAdmin(admin.ModelAdmin): 15 | list_display = ('name', 'balance') 16 | 17 | def get_urls(self): 18 | urls = super().get_urls() 19 | new_urls = [path('upload-csv/', self.upload_csv),] 20 | return new_urls + urls 21 | 22 | def upload_csv(self, request): 23 | 24 | if request.method == "POST": 25 | csv_file = request.FILES["csv_upload"] 26 | 27 | if not csv_file.name.endswith('.csv'): 28 | messages.warning(request, 'The wrong file type was uploaded') 29 | return HttpResponseRedirect(request.path_info) 30 | 31 | file_data = csv_file.read().decode("utf-8") 32 | csv_data = file_data.split("\n") 33 | 34 | for x in csv_data: 35 | fields = x.split(",") 36 | created = customer.objects.update_or_create( 37 | name = fields[0], 38 | balance = fields[1], 39 | ) 40 | url = reverse('admin:index') 41 | return HttpResponseRedirect(url) 42 | 43 | form = CsvImportForm() 44 | data = {"form": form} 45 | return render(request, "admin/csv_upload.html", data) 46 | 47 | admin.site.register(customer, CustomerAdmin) -------------------------------------------------------------------------------- /bank/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class BankConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'bank' 7 | -------------------------------------------------------------------------------- /bank/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.1.7 on 2021-05-18 11:45 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='customer', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('balance', models.DecimalField(decimal_places=2, max_digits=5)), 20 | ('created', models.DateTimeField(auto_now_add=True)), 21 | ('updated', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /bank/migrations/0002_alter_customer_id.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.3 on 2021-05-18 13:16 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('bank', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='customer', 15 | name='id', 16 | field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /bank/migrations/0003_auto_20210531_2054.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.3 on 2021-05-31 19:54 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('bank', '0002_alter_customer_id'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='customer', 15 | name='created', 16 | ), 17 | migrations.RemoveField( 18 | model_name='customer', 19 | name='updated', 20 | ), 21 | ] 22 | -------------------------------------------------------------------------------- /bank/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/migrations/__init__.py -------------------------------------------------------------------------------- /bank/migrations/__pycache__/0001_initial.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/migrations/__pycache__/0001_initial.cpython-39.pyc -------------------------------------------------------------------------------- /bank/migrations/__pycache__/0002_alter_customer_id.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/migrations/__pycache__/0002_alter_customer_id.cpython-39.pyc -------------------------------------------------------------------------------- /bank/migrations/__pycache__/0002_auto_20210518_1233.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/migrations/__pycache__/0002_auto_20210518_1233.cpython-39.pyc -------------------------------------------------------------------------------- /bank/migrations/__pycache__/0003_auto_20210531_2054.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/migrations/__pycache__/0003_auto_20210531_2054.cpython-39.pyc -------------------------------------------------------------------------------- /bank/migrations/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/migrations/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /bank/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | class customer(models.Model): 4 | name = models.CharField(max_length=50) 5 | balance = models.DecimalField(max_digits=5, decimal_places=2) 6 | 7 | def __str__(self): 8 | return self.name -------------------------------------------------------------------------------- /bank/views.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/bank/views.py -------------------------------------------------------------------------------- /core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/core/__init__.py -------------------------------------------------------------------------------- /core/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/core/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /core/__pycache__/settings.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/core/__pycache__/settings.cpython-39.pyc -------------------------------------------------------------------------------- /core/__pycache__/urls.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/core/__pycache__/urls.cpython-39.pyc -------------------------------------------------------------------------------- /core/__pycache__/wsgi.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/core/__pycache__/wsgi.cpython-39.pyc -------------------------------------------------------------------------------- /core/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for core project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /core/settings.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 4 | BASE_DIR = Path(__file__).resolve().parent.parent 5 | 6 | 7 | # Quick-start development settings - unsuitable for production 8 | # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ 9 | 10 | # SECURITY WARNING: keep the secret key used in production secret! 11 | SECRET_KEY = 'django-insecure-ge356@&9l9=djmz073s&r7na@(bhf%r**tho)jt^din*sfs)1w' 12 | 13 | # SECURITY WARNING: don't run with debug turned on in production! 14 | DEBUG = True 15 | 16 | ALLOWED_HOSTS = [] 17 | 18 | 19 | # Application definition 20 | 21 | INSTALLED_APPS = [ 22 | 'django.contrib.admin', 23 | 'django.contrib.auth', 24 | 'django.contrib.contenttypes', 25 | 'django.contrib.sessions', 26 | 'django.contrib.messages', 27 | 'django.contrib.staticfiles', 28 | 'bank', 29 | ] 30 | 31 | MIDDLEWARE = [ 32 | 'django.middleware.security.SecurityMiddleware', 33 | 'django.contrib.sessions.middleware.SessionMiddleware', 34 | 'django.middleware.common.CommonMiddleware', 35 | 'django.middleware.csrf.CsrfViewMiddleware', 36 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 37 | 'django.contrib.messages.middleware.MessageMiddleware', 38 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 39 | ] 40 | 41 | ROOT_URLCONF = 'core.urls' 42 | 43 | TEMPLATES = [ 44 | { 45 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 46 | 'DIRS': [BASE_DIR / 'templates'], 47 | 'APP_DIRS': True, 48 | 'OPTIONS': { 49 | 'context_processors': [ 50 | 'django.template.context_processors.debug', 51 | 'django.template.context_processors.request', 52 | 'django.contrib.auth.context_processors.auth', 53 | 'django.contrib.messages.context_processors.messages', 54 | ], 55 | }, 56 | }, 57 | ] 58 | 59 | WSGI_APPLICATION = 'core.wsgi.application' 60 | 61 | DATABASES = { 62 | 'default': { 63 | 'ENGINE': 'django.db.backends.sqlite3', 64 | 'NAME': BASE_DIR / 'db.sqlite3', 65 | } 66 | } 67 | 68 | AUTH_PASSWORD_VALIDATORS = [ 69 | { 70 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 71 | }, 72 | { 73 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 74 | }, 75 | { 76 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 77 | }, 78 | { 79 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 80 | }, 81 | ] 82 | 83 | LANGUAGE_CODE = 'en-us' 84 | 85 | TIME_ZONE = 'UTC' 86 | 87 | USE_I18N = True 88 | 89 | USE_L10N = True 90 | 91 | USE_TZ = True 92 | 93 | STATIC_URL = '/static/' 94 | 95 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 96 | -------------------------------------------------------------------------------- /core/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path 3 | from bank import views 4 | 5 | urlpatterns = [ 6 | path('admin/', admin.site.urls), 7 | ] 8 | -------------------------------------------------------------------------------- /core/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for core project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/db.sqlite3 -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asgiref==3.3.4 2 | Django==3.2.3 3 | pytz==2021.1 4 | sqlparse==0.4.1 5 | -------------------------------------------------------------------------------- /starting_app.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_Admin_csv_Button_Upload/0d329424c2631d88701d300db0262b904ace8ea9/starting_app.zip -------------------------------------------------------------------------------- /templates/admin/bank/customer/change_list.html: -------------------------------------------------------------------------------- 1 | {% extends "admin/change_list.html" %} 2 | {% load static %} 3 | {% block content %} 4 | 5 | Upload a csv file 6 | 7 | {{ block.super }} 8 | {% endblock %} -------------------------------------------------------------------------------- /templates/admin/csv_upload.html: -------------------------------------------------------------------------------- 1 | {% extends 'admin/base.html' %} 2 | 3 | {% block content %} 4 |
5 |
6 | {{ form.as_p }} 7 | {% csrf_token %} 8 | 9 |
10 |
11 | {% endblock %} --------------------------------------------------------------------------------