from django.views.generic import View, ListView, DetailView
class AddUserPhoneToContextMixin(View):
def get_context_data(self, **kwargs):
'''
Add phone number to context data
'''
ctx = super().get_context_data(**kwargs)
try:
user = User.objects.get(pk=1)
ctx['phone_number'] = user.userprofile.phone_number
finally:
return ctx
class ItemListView(AddUserPhoneToContextMixin, ListView):
model = Item
paginate_by = 9
class ItemDetailView(AddUserPhoneToContextMixin, DetailView):
model = Item
try:
phone_number = user.userprofile.phonenumber
except UserProfile.DoesNotExists:
raise NotFound('Warning! User profile did not created!')
class Model2(models.Model):
m2m_field = models.ManyToManyField(to=Model1, related_name='second_models')
m1 = Model1.object.get(...) #он просто у меня есть
second_model_queryset = m1.second_models.all()
def get(self, request, **kwargs):
if not request.user.is_staff:
raise PermissionDenied
super().get(request, **kwargs)
# my_project/my_app/utils.py
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
if response.status_code == 500:
# My custom 500 error handler.
pass
return response
# settings.py
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
class ФормаСортировки(forms.Form):
список_цен = [
('no', 'не важно'),
('little', 'сначала дешёвые'),
('big', 'сначала дорогие'),
]
категория = forms.ChoiceField(label= '', initial= 'цена', choices= список_цен)
class MyFuckingView(TemplateView):
template_name = 'some/module/template.html'
class MyFuckingView(TemplateView):
template_name = 'some/module/template.html'
def get_context(self):
ctx = super().get_context()
ctx['model_name'] = 'Sasha Grey'
ctx['category'] = 'Milf anal'
return ctx
{% if request.user.is_admin %}
<a href="#" class="edit-button">Edit</a>
{% endif %}
Это ассоциативный массив.
Функция render() как я понял, по итогу возвращает ответ в виде JSON'a из массива.
def handler(request, *args, **kwargs) -> HttpResponse:
pass
if DEBUG is False:
....
else:
....
if DEBUG:
# some code if DEBUG is True
else:
# some code if DEBUG is False
setattr(request, 'company', company_instance)
request.company
setattr(request, 'main_site', True)
Company.objects.create(
owner=some_user,
domain=company_domain,
email=company_email,
...
created=timezone.now()
)