from django.shortcuts import render
from django.db.models import Count
from django.db.models.functions import TruncMonth
from .models import Article
def group_by_month(request):
groups = Article.objects
.annotate(month=TruncMonth('publication_date')) # Извлекаем месяц из даты
.values('month') # Группируем по месяцам
.annotate(c=Count('id')) # Количество статей в месяце
return render(request, 'monthes.html', {'monthes': groups})
def articles_in_month(request, month):
articles = Article.objects.filter(publication_date__month=month)
return render(request, 'articles', {'articles': articles})
var fileField = document.getElementById('image');
var preview = document.getElementById('preview');
fileField.addEventListener('change', function(event) {
var reader = new FileReader();
reader.onload = function(event) {
preview.setAttribute('src', event.target.result);
}
reader.readAsDataURL(event.target.files[0]);
}, false);
with open("image.png", "wb") as fh:
fh.write(request.POST['image'].decode('base64'))
import base64
with open("image.png", "wb") as fh:
fh.write(base64.decodestring(request.POST['image']))
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
class Department {
private String number;
private String name;
private List<Integer> products;
public Department(String number, String name) {
this.number = number;
this.name = name;
this.products = new ArrayList<>();
}
public void setProducts(List<Integer> products) {
this.products = products;
}
public String toString() {
return String.format("Name: %s\nNumber: %s\nProducts: %s\n", name, number, products);
}
}
public class SAXHandler extends DefaultHandler {
private List<Department> departments;
private List<Integer> products;
private Department currentDepartment;
public SAXHandler() {
this.departments = new ArrayList<>();
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equals("department")) {
String name = attributes.getValue("name");
String number = attributes.getValue("number");
products = new ArrayList<>();
currentDepartment = new Department(name, number);
}
if(qName.equals("product")) {
String id = attributes.getValue("id");
products.add(Integer.valueOf(id));
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equals("department")) {
currentDepartment.setProducts(products);
departments.add(currentDepartment);
}
}
public List<Department> getResult() {
return departments;
}
public static void main(final String args[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
SAXHandler handler = new SAXHandler();
saxParser.parse("test.xml", handler);
handler.getResult().stream().forEach(System.out::println);
}
catch(SAXException ex) {}
catch(ParserConfigurationException ex) {}
catch(IOException ex) {}
}
}
Map<String, String> attrs = new HashMap<>();
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
String name = attributes.getValue("name");
String value = attributes.getValue("size");
attrs.put(name, value);
}