Advertisement
horozov86

Creating app photo

Mar 18th, 2024
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. Tell me how I can modify my project my_holiday, so I can add additional logic with button Add Photo to the Place for example?
  2.  
  3. models.py
  4.  
  5. from django.db import models
  6.  
  7. class Place(models.Model):
  8.     name = models.CharField(max_length=200)
  9.     location = models.CharField(max_length=200)
  10.     description = models.TextField()
  11.  
  12.     def __str__(self):
  13.         return self.name
  14.  
  15. class Photo(models.Model):
  16.     image = models.ImageField(upload_to='place_photos/')
  17.     places = models.ManyToManyField(Place, related_name='photos')
  18.  
  19.     def __str__(self):
  20.         return f"Photo {self.id}"
  21.  
  22.  
  23. views.py
  24.  
  25. class PhotoCreateView(CreateView):
  26.     model = Photo
  27.     form_class = PhotoForm
  28.     template_name = 'photo_create.html'
  29.  
  30.     def form_valid(self, form):
  31.         place_id = self.request.GET.get('place_id')
  32.         place = get_object_or_404(Place, pk=place_id)
  33.         form.instance.place = place
  34.         return super().form_valid(form)
  35.  
  36. class PhotoEditView(UpdateView):
  37.     model = Photo
  38.     form_class = PhotoForm
  39.     template_name = 'photo_edit.html'
  40.  
  41. class PhotoDetailsView(DetailView):
  42.     model = Photo
  43.     template_name = 'photo_details.html'
  44.  
  45. photo_create.html
  46.  
  47. {% extends 'base.html' %}
  48.  
  49. {% block main_content %}
  50.     <h1>Add Photo</h1>
  51.     <form method="post" enctype="multipart/form-data">
  52.         {% csrf_token %}
  53.         {{ form.as_p }}
  54.         <button type="submit">Submit</button>
  55.     </form>
  56. {% endblock %}
  57.  
  58.  
  59. photo_edit.html
  60.  
  61. {% extends 'base.html' %}
  62.  
  63. {% block main_content %}
  64.     <h1>Edit Photo</h1>
  65.     <form method="post" enctype="multipart/form-data">
  66.         {% csrf_token %}
  67.         {{ form.as_p }}
  68.         <button type="submit">Update</button>
  69.     </form>
  70. {% endblock %}
  71.  
  72.  
  73. photo_details.html
  74.  
  75. {% extends 'base.html' %}
  76.  
  77. {% block main_content %}
  78.     <h1>Photo Details</h1>
  79.     <p>Image: <img src="{{ object.image.url }}" alt="{{ object.place.location }}"></p>
  80.     <p>Place: {{ object.place.location }}</p>
  81. {% endblock %}
  82.  
  83. travelogue_view.html
  84.  
  85. {% extends 'base.html' %}
  86.  
  87. {% block main_content %}
  88.     {% if places %}
  89.         <h1>Total places: {{ places|length }}</h1>
  90.         <section id="travelogue">
  91.             {% for place in places %}
  92.             <div class="place-card">
  93.                 <div class="image-wrap">
  94.                     <!-- Display all photos associated with the place -->
  95.                     {% for photo in place.photos.all %}
  96.                         <img src="{{ photo.image.url }}" alt="{{ place.location }}">
  97.                     {% endfor %}
  98.                 </div>
  99.                 <h3>Location: {{ place.location }}</h3>
  100.                 <h1>Category: {{ place.category }}</h1>
  101.                 <div class="details-buttons">
  102.                     <a href="{% url 'details_place' pk=place.pk %}" class="details-btn">Details</a>
  103.                 </div>
  104.                 <!-- Add a button to allow users to add a photo to this place -->
  105.                 <a href="{% url 'photo_create' %}?place_id={{ place.pk }}" class="add-photo-btn">Add Photo</a>
  106.             </div>
  107.             {% endfor %}
  108.         </section>
  109.     {% else %}
  110.         <p class="no-articles">No places yet</p>
  111.     {% endif %}
  112. {% endblock %}
  113.  
  114. urls.py
  115.  
  116. from django.urls import path
  117. from .views import PhotoCreateView, PhotoEditView, PhotoDetailsView
  118.  
  119. urlpatterns = [
  120.     path('photo/create/', PhotoCreateView.as_view(), name='photo_create'),
  121.     path('photo/<int:pk>/edit/', PhotoEditView.as_view(), name='photo_edit'),
  122.     path('photo/<int:pk>/', PhotoDetailsView.as_view(), name='photo_details'),
  123. ]
  124.  
  125.  
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement