Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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?
- models.py
- from django.db import models
- class Place(models.Model):
- name = models.CharField(max_length=200)
- location = models.CharField(max_length=200)
- description = models.TextField()
- def __str__(self):
- return self.name
- class Photo(models.Model):
- image = models.ImageField(upload_to='place_photos/')
- places = models.ManyToManyField(Place, related_name='photos')
- def __str__(self):
- return f"Photo {self.id}"
- views.py
- class PhotoCreateView(CreateView):
- model = Photo
- form_class = PhotoForm
- template_name = 'photo_create.html'
- def form_valid(self, form):
- place_id = self.request.GET.get('place_id')
- place = get_object_or_404(Place, pk=place_id)
- form.instance.place = place
- return super().form_valid(form)
- class PhotoEditView(UpdateView):
- model = Photo
- form_class = PhotoForm
- template_name = 'photo_edit.html'
- class PhotoDetailsView(DetailView):
- model = Photo
- template_name = 'photo_details.html'
- photo_create.html
- {% extends 'base.html' %}
- {% block main_content %}
- <h1>Add Photo</h1>
- <form method="post" enctype="multipart/form-data">
- {% csrf_token %}
- {{ form.as_p }}
- <button type="submit">Submit</button>
- </form>
- {% endblock %}
- photo_edit.html
- {% extends 'base.html' %}
- {% block main_content %}
- <h1>Edit Photo</h1>
- <form method="post" enctype="multipart/form-data">
- {% csrf_token %}
- {{ form.as_p }}
- <button type="submit">Update</button>
- </form>
- {% endblock %}
- photo_details.html
- {% extends 'base.html' %}
- {% block main_content %}
- <h1>Photo Details</h1>
- <p>Image: <img src="{{ object.image.url }}" alt="{{ object.place.location }}"></p>
- <p>Place: {{ object.place.location }}</p>
- {% endblock %}
- travelogue_view.html
- {% extends 'base.html' %}
- {% block main_content %}
- {% if places %}
- <h1>Total places: {{ places|length }}</h1>
- <section id="travelogue">
- {% for place in places %}
- <div class="place-card">
- <div class="image-wrap">
- <!-- Display all photos associated with the place -->
- {% for photo in place.photos.all %}
- <img src="{{ photo.image.url }}" alt="{{ place.location }}">
- {% endfor %}
- </div>
- <h3>Location: {{ place.location }}</h3>
- <h1>Category: {{ place.category }}</h1>
- <div class="details-buttons">
- <a href="{% url 'details_place' pk=place.pk %}" class="details-btn">Details</a>
- </div>
- <!-- Add a button to allow users to add a photo to this place -->
- <a href="{% url 'photo_create' %}?place_id={{ place.pk }}" class="add-photo-btn">Add Photo</a>
- </div>
- {% endfor %}
- </section>
- {% else %}
- <p class="no-articles">No places yet</p>
- {% endif %}
- {% endblock %}
- urls.py
- from django.urls import path
- from .views import PhotoCreateView, PhotoEditView, PhotoDetailsView
- urlpatterns = [
- path('photo/create/', PhotoCreateView.as_view(), name='photo_create'),
- path('photo/<int:pk>/edit/', PhotoEditView.as_view(), name='photo_edit'),
- path('photo/<int:pk>/', PhotoDetailsView.as_view(), name='photo_details'),
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement