South in the early winter of 1858 and the following spring of 1859, two years before the outbreak of the American Civil War. With the help of a German bounty hunter (Waltz), a freed slave (Foxx) sets out to rescue his wife from a brutal Mississippi plantation owner (DiCaprio).
a job as a clerk at Video Archives in Manhattan Beach. - Made his way in the industry by writing the screenplay to 'True Romance' and 'Natural Born Killers'
framework, written in Python. - Born in 2003, when programmers at the Lawrence Journal-World newspaper began using Python to build its website. It was released publicly in 2005.
perfectionists with deadlines’. - Some popular sites that use Django: Pinterest, Instagram, Mozilla, Disqus, Bitbucket, Pitchfork, National Geographic.
delivery boy'), ('PILOT', 'spaceship pilot'), ('BENDING', 'bending unit'), ('DOCTOR', 'doctor'), ) role = models.CharField(max_length=16, choices=ROLE_CHOICES) def __str__(self): return self.name Models Models are Python classes that extend models.Model. Every field is an instance of models.Field type object. You can also create your own fields.
Employee manage.py@planet-express > migrate Running migrations: Rendering model states... DONE Applying employees.0001_initial... OK Migrations Migrations are done automatically and incrementally, with every change applied to application models.
(most recent call last): ... django.core.exceptions.ValidationError: {'__all__': ['Whoop, whoop, whoop!']} Model validation Model validation is extremely easy and can be used to check and inspect the data throughout the application, whether you create objects from code, shell or forms. # In models ... def clean(self): if ('Zoidberg' in self.name and self.role != 'DOCTOR'): raise ValidationError( "Whoop, whoop, whoop!")
from users, so Django made it easy to create and manage them from within the application. class PlanetExpressContactForm(forms.Form): subject = forms.CharField( max_length=100, ) message = forms.CharField( widget=forms.Textarea, ) sender = forms.EmailField() cc_myself = forms.BooleanField( required=False, )
users, so Django made it easy to create and manage them from within the application. Forms # In view … if form.is_valid(): data = form.cleaned_data subject = data['subject'] message = data['message'] sender = data['sender'] cc_myself = data['cc_myself'] recipients = ['[email protected]'] if cc_myself: recipients.append(sender) send_mail(subject, message, sender, recipients)
{% csrf_token %} {{ form }} <input type="submit" value="Submit" /> </form> Forms also have built in rendering. {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }} {{ field }} {% if field.help_text %} <p class="help"> {{ field.help_text|safe }} </p> {% endif %} </div> {% endfor %}
'role'] widgets = { 'role': forms.RadioSelect, } Model Forms Model forms automatically create a form for provided model, with its data validation and default widgets. class EmployeeForm(forms.ModelForm): class Meta: model = Employee fields = '__all__'
which data you see, not how you see it - separates content from presentation MVC’s controller: - translates interactions with the view into actions to be performed by the model
you to do the most common scenarios in couple lines of code, nice and easy. Class Based Views def employees_list_view(request): template_name = 'employees/list.html' ctx = dict() # Employees ctx['employees'] = Employee.objects.all() return render(request, template_name, ctx) class EmployeesListView(ListView): template_name = 'employees/list.html' model = Employee
done in 40 lines of code. Class Based Views class EmployeeUpdateView(UpdateView): template_name = 'employee/create.html' model = Employee fields = '__all__' success_url = reverse('index') class EmployeeDeleteView(DeleteView): template_name = 'employee/delete.html' model = Employee success_url = reverse('index') class EmployeeCreateView(CreateView): template_name = 'employee/create.html' model = Employee fields = '__all__' success_url = reverse('index')
are extendable and very easy to modify. Class Based Views class UserProfileView(DetailView): template_name = 'user/profile.html' model = User def get_object(queryset=None): return self.request.user def get_context_data(self, **kwargs): role = self.request.user.role kwargs['employees'] = Employee. objects.filter(role=role) return super().get_context_data( **kwargs)
inherit from Mixins, which are classes that usually add one, extra functionality (the hint is usually in the name). Class Based Views class EmployeesListView(UserPassesTestMixin, ListView): template_name = 'employees/list.html' model = Employee def test_func(self): user = self.request.user return user.email.endswith( '@pythonity.com') class EmployeesListView(LoginRequiredMixin, ListView): template_name = 'employees/list.html' model = Employee
Permission models - All basic forms and views - such as password_reset, UserCreationForm, etc. - are already implemented. - Every model has add, change and delete permission by default.
Yes, you can also easily add your own. Sample commands: startproject, createsuperuser, runserver, dumpdata, loaddata, makemessages, collectstatic, migrate, makemigrations, etc.