adsfasdf

parent 7f956a25
...@@ -34,7 +34,7 @@ class Migration(migrations.Migration): ...@@ -34,7 +34,7 @@ class Migration(migrations.Migration):
), ),
migrations.AddField( migrations.AddField(
model_name='article', model_name='article',
name='tags', name='templatetags',
field=models.ManyToManyField(blank=True, related_name='tags', through='articles.ArticleTag', to='articles.Tag'), field=models.ManyToManyField(blank=True, related_name='templatetags', through='articles.ArticleTag', to='articles.Tag'),
), ),
] ]
...@@ -12,7 +12,7 @@ class Migration(migrations.Migration): ...@@ -12,7 +12,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.AlterField( migrations.AlterField(
model_name='article', model_name='article',
name='tags', name='templatetags',
field=models.ManyToManyField(blank=True, related_name='articles', through='articles.ArticleTag', to='articles.Tag'), field=models.ManyToManyField(blank=True, related_name='articles', through='articles.ArticleTag', to='articles.Tag'),
), ),
] ]
...@@ -12,7 +12,7 @@ class Migration(migrations.Migration): ...@@ -12,7 +12,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.RemoveField( migrations.RemoveField(
model_name='article', model_name='article',
name='tags', name='templatetags',
), ),
migrations.AddField( migrations.AddField(
model_name='article', model_name='article',
......
...@@ -12,7 +12,7 @@ class Migration(migrations.Migration): ...@@ -12,7 +12,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name='article', model_name='article',
name='tags', name='templatetags',
field=models.ManyToManyField(blank=True, related_name='articles', to='articles.Tag'), field=models.ManyToManyField(blank=True, related_name='articles', to='articles.Tag'),
), ),
migrations.AlterField( migrations.AlterField(
......
{% load static %} {% load static %}
{% load any_tag %}
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
...@@ -12,9 +13,11 @@ ...@@ -12,9 +13,11 @@
</head> </head>
<body> <body>
{% include 'partial/navbar.html' %} {% include 'partial/navbar.html' %}
{% any_function %}
{% some_function %}
<div class="container"> <div class="container">
{% block title %}{% endblock %} {% block title %}{% endblock %}
{% include 'partial/messages.html' %}
<ul> <ul>
{% block menu_links %} {% block menu_links %}
...@@ -24,5 +27,7 @@ ...@@ -24,5 +27,7 @@
{% block content %} {% block content %}
{% endblock %} {% endblock %}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{% static 'js/script.js' %}"></script>
</body> </body>
</html> </html>
\ No newline at end of file
{% if messages %}
{% for message in messages %}
<div class="alert alert-dismissible fade show {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}
alert-danger
{% elif message.level == DEFAULT_MESSAGE_LEVELS.WARNING %}
alert-warning
{% elif message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %}
alert-success
{% elif message.level == DEFAULT_MESSAGE_LEVELS.INFO %}
alert-primary
{% else %}
alert-secondary
{% endif %}" role="alert">
{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
{% endif %}
\ No newline at end of file
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
{% if page_obj.has_previous %} {% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">Назад</a> <a href="?page={{ page_obj.previous_page_number }}">Назад</a>
{% else %} {% else %}
<span class="page-disabled">Назад</span> <span class="page-disabled" id="test">Назад</span>
{% endif %} {% endif %}
<form class="current-page" method="get"> <form class="current-page" method="get">
......
from django import template
register = template.Library()
@register.simple_tag
def any_function():
return "My Tag text"
@register.simple_tag
def some_function():
return "My Tag Some text"
\ No newline at end of file
...@@ -16,7 +16,7 @@ from .views.author_views import ( ...@@ -16,7 +16,7 @@ from .views.author_views import (
AuthorUpdateView, AuthorUpdateView,
AuthorListView, AuthorListView,
AuthorDeleteView, AuthorDeleteView,
AuthorCreateView AuthorCreateView, json_echo_view, get_token_view, article_api_list_view, article_api_create_view
) )
...@@ -86,6 +86,13 @@ author_routes = [ ...@@ -86,6 +86,13 @@ author_routes = [
) )
] ]
api = [
path('echo/', json_echo_view),
path('get_csrf/', get_token_view),
path('api/articles/list', article_api_list_view),
path('api/articles/create', article_api_create_view)
]
urlpatterns = comments_routes urlpatterns = comments_routes
urlpatterns += author_routes urlpatterns += author_routes
urlpatterns += articles_routes urlpatterns += articles_routes
urlpatterns += api
import json
from datetime import datetime
from urllib.parse import urlencode from urllib.parse import urlencode
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.serializers import serialize, deserialize
from django.db.models import Q from django.db.models import Q
from django.http import HttpResponse, HttpResponseNotAllowed, JsonResponse
from django.shortcuts import get_object_or_404, render, redirect from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.views.decorators.csrf import csrf_protect from django.views.decorators.csrf import csrf_protect, ensure_csrf_cookie
from django.views.generic import FormView, DeleteView from django.views.generic import FormView, DeleteView
from articles.forms import SearchForm, ArticleForm, CommentForm from articles.forms import SearchForm, ArticleForm, CommentForm
...@@ -25,6 +29,8 @@ class ArticleListView(ListView): ...@@ -25,6 +29,8 @@ class ArticleListView(ListView):
paginate_orphans = 1 paginate_orphans = 1
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
request.session['key'] = reverse("author_list")
print(request.session.items())
self.form = self.get_search_form() self.form = self.get_search_form()
self.search_value = self.get_search_value() self.search_value = self.get_search_value()
return super().get(request, *args, **kwargs) return super().get(request, *args, **kwargs)
...@@ -62,6 +68,7 @@ class ArticleCreateView(LoginRequiredMixin, CreateView): ...@@ -62,6 +68,7 @@ class ArticleCreateView(LoginRequiredMixin, CreateView):
def get_redirect_url(self): def get_redirect_url(self):
return reverse('article_detail', kwargs={'pk': self.object.pk}) return reverse('article_detail', kwargs={'pk': self.object.pk})
class ArticleUpdateView(PermissionRequiredMixin, LoginRequiredMixin, UpdateView): class ArticleUpdateView(PermissionRequiredMixin, LoginRequiredMixin, UpdateView):
model = Article model = Article
template_name = "articles/update.html" template_name = "articles/update.html"
...@@ -99,4 +106,5 @@ class ArticleDeleteView(DeleteView): ...@@ -99,4 +106,5 @@ class ArticleDeleteView(DeleteView):
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
return self.delete(request, *args, **kwargs) messages.add_message(self.request, messages.SUCCESS, f"Запись {kwargs.get('pk')} удалена!")
return self.delete(request, *args, **kwargs)
\ No newline at end of file
import json
from datetime import datetime
from django.core.serializers import serialize, deserialize
from django.http import HttpResponse, HttpResponseNotAllowed, JsonResponse
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.views.decorators.csrf import ensure_csrf_cookie
from articles.forms import AuthorForm from articles.forms import AuthorForm
from articles.helpers.views import ListView, CreateView, UpdateView, DeleteView from articles.helpers.views import ListView, CreateView, UpdateView, DeleteView
from articles.models import Author from articles.models import Author, Article
class AuthorListView(ListView): class AuthorListView(ListView):
...@@ -32,3 +38,46 @@ class AuthorDeleteView(DeleteView): ...@@ -32,3 +38,46 @@ class AuthorDeleteView(DeleteView):
confirm_deletion = False confirm_deletion = False
redirect_url = reverse_lazy('author_list') redirect_url = reverse_lazy('author_list')
@ensure_csrf_cookie
def get_token_view(request, *args, **kwargs):
if request.method == 'GET':
return HttpResponse()
return HttpResponseNotAllowed('Разрешен только GET метод')
def json_echo_view(request, *args, **kwargs):
answer = {
'time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'method': request.method
}
if request.body:
answer['content'] = json.loads(request.body)
answer_to_json = json.dumps(answer)
response = HttpResponse(answer_to_json)
response['Content-Type'] = 'application/json'
return response
def article_api_list_view(request, *args, **kwargs):
if request.method == 'GET':
articles = Article.objects.all()
articles_data = serialize('json', articles)
print(articles_data)
return HttpResponse(articles_data)
def article_api_create_view(request, *args, **kwargs):
if request.method == 'POST':
if request.body:
article_data = deserialize('json', request.body)
for item in article_data:
item.save()
return JsonResponse({'id': item.object.id})
response = JsonResponse({'error': 'No data provided!'})
response.status_code = 400
return response
$(document).ready(function () {
$("body").css('background', 'white');
let paginator = $('.pagination');
let children = paginator.children().children('a, span')
children.css('background', 'red')
let elem = $('#test');
elem.on('click', function (e) {
$(this).parents('.container').find('thead').css('color', 'red');
$(this).parents('step-links').html("<button>ТЕкст</button>");
$(this).parent().append("<button>Текст</button>")
});
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment