Open anndoc opened 3 years ago
1. Как из функции которая выдает csv файл сделать класс. Со стандартными джанговских классами все более-менее понятно, а если свой пишешь? Все что успел нагуглить до того как ебнул инет было про отрисовку списков или еще чего-нибудь, все наследовалось от ListView, CreateView и т.д.
In case if you need to write some custom view you can inherit from django.views.View
and implement there get
/ post
methods, for example
from django.views import View
class UserCSVImportView(View):
def get(self, request, *args, **kwargs):
response = HttpResponse(
content_type='text/csv',
headers={'Content-Disposition': 'attachment; filename="Users.csv"'},
)
writer = csv.writer(response)
writer.writerow(['Username', 'Birth date', 'Age validation', 'Random number', 'BizzFuzz'])
for user in User.objects.all():
writer.writerow([user.username, ])
return response
Since you need to export list of users you can inherit from django.views.generic.list.BaseListView
. It's already has get_queryset method so you don't need to implement own
from django.views.generic.list import BaseListView
class UserCSVImportView(BaseListView):
model = User
filename = 'Users.csv'
fields = ('Username', 'Birth date', 'Age validation', 'Random number', 'BizzFuzz')
def get(self, request, *args, **kwargs):
response = HttpResponse(
content_type='text/csv',
headers={'Content-Disposition': f'attachment; filename="{self.filename}"'},
)
writer = csv.writer(response)
writer.writerow(self.fields)
for user in self.get_queryset():
writer.writerow([user.username])
return response
2. Второй момент это как на стандартные эти классы натянуть стили? Если посмотришь в создании пользователя поля стандартные. Из-за того что там form.as_p получилось сделать полукостыль в виде стиля тега <p>
в style css
You can get access to the fields in the template in a loop {% for field in form %}
. In this case you will able to customize general form but not add styles to the each form input.
If that's not enough there are bunch of django apps which allow you to add css class / attributes to the form fields. I used to use the django-widget-tweaks https://github.com/jazzband/django-widget-tweaks , not sure if it's still popular. If you were able to get the latest version of Two Scoops of Django books you can check there what they recommend.
{% extends 'base.html' %}
{% load i18n %}
{% load widget_tweaks %}
{% block content %}
<form class="needs-validation{% if form.errors %} was-validated{% endif %}" novalidate
method="post"
action="{% if object %}{% url 'users:update' object.pk %}{% else %}{% url 'users:create' %}{% endif %}">
{% for field in form %}
<div class="col-md-4 mb-3">
<label class="control-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
{% render_field field placeholder=field.label class='form-control' %}
{% if field.errors %}
<div class="invalid-feedback">
{% for error in field.errors %}
{{ error }}<br/>
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
<div class="col-md-4 mb-3">
<button class="btn btn-primary" type="submit">{% trans 'Save' %}</button>
</div>
</form>
{% endblock content %}
.idea, venv, __pycache__, *.pyc
should not be committed.requirements.txt
(Two Scoops of Django book chapter 5.5 http://bedford-computing.co.uk/learning/wp-content/uploads/2016/08/Daniel-Roy-Greenfeld-Audrey-Roy-Greenfeld-Two-Scoops-of-Django_-Best-Practices-for-Django-1.8-Two-Scoops-Press-2015.pdf )get_user_model()
insteadsettings.DEBUG, settings.ALLOWED_HOSTS
) should be stored in the.env
file. This is bad practiceuse django-environ instead https://django-environ.readthedocs.io/en/latest/
13
is a magic number, it's better to create a variable, for exampleUSER_ALLOWED_AGE
, in the settings or in theconstants.py
file for that.class DataMixin(object):
justclass DataMixin:
object is redundant in the python3paginate_by
to the list view. Just to avoid retrieving thousand users.Tests:
use factory-boy https://factoryboy.readthedocs.io/en/stable/ It will be something like:
import factory from django.contrib.auth import get_user_model TEST_USER_PASSWORD = 'P@ssw0rdP@ssw0rd' User = get_user_model()
class UserFactory(factory.django.DjangoModelFactory): email = factory.Sequence(lambda n: f'user-{n}-{random.randint(1, 1000)}@example.com') password = factory.PostGenerationMethodCall('set_password', TEST_USER_PASSWORD) first_name = factory.Faker('first_name') last_name = factory.Faker('last_name')