Форум сайта python.su
Здравствуйте!
когда то давно я сделал на django кнопку (без views.py! - все в urls.py),
при нажатии на которую будет какая то обработка. :-)
Your text to link here…
urls.py:
from django.contrib import admin from django.urls import path # =========================================>>>>>>>>>>>>>> from django.http import HttpResponse # !!! new (01) k = 0 def home(request): global k k += 1 # stroka = "Привет!!! ===> {0}".format(k) return HttpResponse("<h1>{1}</h1><hr> \ <form><input type='submit' value='{0}'> \ </form>".format(stroka, k)) urlpatterns = [ path('admin/', admin.site.urls), path('', home), ]
from django.db import models # Create your models here. from django.contrib import admin from django.utils.html import format_html class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) color_code = models.CharField(max_length=6) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.mycount1 = 0 def colored_name(self): return format_html('<span style="color: #{};">{} {}</span>', self.color_code, self.first_name, self.last_name) def mycounter(self): self.mycount1 += 1 return format_html('<form method="post" action="."> \ <input type="submit" value="жми кнопку"></form><hr> \ <h1 style="color: #{};">{} </h1>', self.color_code, self.mycount1) class PersonAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'colored_name', 'mycounter')
from django.contrib import admin # Register your models here. from .models import Person, PersonAdmin admin.site.register(Person, PersonAdmin)
from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]
Отредактировано Koala (Март 5, 2019 15:58:34)
Прикреплённый файлы:
dj_button_admin.7z (513,8 KБ)
Офлайн