Guest User

Untitled

a guest
Sep 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. class Post(models.Model):
  2. title = models.CharField(max_length=255)
  3. content = models.TextField()
  4. image = models.ImageField(blank=True, null=True)
  5. publication_date = models.DateField(default=datetime.date.today)
  6. expiring_date = models.DateField()
  7.  
  8. class PostForm(forms.models.ModelForm):
  9.  
  10. def __init__(self, *args, **kwargs):
  11. super().__init__(*args, **kwargs)
  12. self.instance.publication_date = datetime.date.today()
  13.  
  14. class Meta:
  15. model = Post
  16. exclude = ['publication_date']
  17. widgets = {
  18. 'title': forms.fields.TextInput(attrs={
  19. 'class': 'form-control'
  20. }),
  21. 'content': forms.Textarea(attrs={
  22. 'class': 'form-control'
  23. }),
  24. 'image': forms.fields.FileInput(attrs={
  25. 'class': 'form-control'
  26. }),
  27. 'expiring_date': forms.fields.DateInput(attrs={
  28. 'input_type': 'date',
  29. 'class': 'form-control'
  30. }),
  31. }
  32.  
  33. def edit(request, post_id):
  34. post = get_object_or_404(Post, pk=post_id)
  35. form = PostForm(instance=post)
  36. return render(request, 'posts/edit.html', {'form': form})
  37.  
  38. {% block content %}
  39. <div class="row">
  40. <div class="col-md-12">
  41. <div class="card animated materialU animation-delay-5">
  42. <div class="card-block card-block-big">
  43. <form action="{% url 'posts:update' %}" method="post" enctype='multipart/form-data'>
  44. {{ form }}
  45. <input type="submit" name="save changes" value="Save Changes" class="btn btn-success">
  46. {% csrf_token %}
  47. </form>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. {% endblock %}
Add Comment
Please, Sign In to add comment