add files

add files
This commit is contained in:
2019-04-26 19:37:41 -06:00
parent fb3c6f1139
commit 7edeb1c36c
17 changed files with 309 additions and 0 deletions
View File
+15
View File
@@ -0,0 +1,15 @@
from django.contrib import admin
from .models import *
# Register your models here.
class CategoryAdmin(admin.ModelAdmin):
pass
class SubCategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'parent')
admin.site.register(Category, CategoryAdmin)
admin.site.register(SubCategory, SubCategoryAdmin)
+5
View File
@@ -0,0 +1,5 @@
from django.apps import AppConfig
class AjaxConfig(AppConfig):
name = 'ajax'
View File
+18
View File
@@ -0,0 +1,18 @@
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class SubCategory(models.Model):
parent = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=20)
def __str__(self):
return self.name
+8
View File
@@ -0,0 +1,8 @@
from .models import *
from rest_framework import serializers
class SubCategoriesSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
+39
View File
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<title>Title</title>
</head>
<body>
<div style="position:absolute; left: 46%; top:30%;">
<select name="categories" id="categories" onchange="populate(this)">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.name }}</option>
{% endfor %}
</select>
<br>
<br>
<select name="subcategories" id="subcategories">
{% for subcategory in subcategories %}
<option value="{{ subcategory.id }}">{{ subcategory.name }}</option>
{% endfor %}
</select>
<script>
function populate(category){
var dynUrl = "{% url 'subcategories-filtered' 0 %}";
$('#subcategories').empty();
$.ajax({
type: "GET",
url: dynUrl.replace('0', category.value),
success: function(opts){
$.each(opts, function(i, opt) {
$('#subcategories').append('<option value="' + opt['id'] + '">' + opt['name'] + '</option>');
});
}
});
}
</script>
</div>
</body>
</html>
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+9
View File
@@ -0,0 +1,9 @@
from django.urls import path
from .views import *
urlpatterns = [
path('', Index.as_view(), name='index'),
path('subcategories/', SubCategories.as_view(), name='subcategories'),
path('subcategories/<int:parent>/', SubCategories.as_view(), name='subcategories-filtered'),
]
+22
View File
@@ -0,0 +1,22 @@
from django.shortcuts import render
from rest_framework import generics
from .models import *
from .serializers import SubCategoriesSerializer
from django.views.generic.edit import View
# Create your views here.
class SubCategories(generics.ListAPIView):
serializer_class = SubCategoriesSerializer
def get_queryset(self):
if 'parent' in self.kwargs:
return SubCategory.objects.filter(parent=self.kwargs['parent'])
return SubCategory.objects.all()
class Index(View):
def get(self, request, *args, **kwargs):
categories = Category.objects.all()
subcategories = SubCategory.objects.filter(parent=categories[0])
return render(request, 'ajax/index.html', {'categories': categories, 'subcategories': subcategories})