Python Forum
get_absolute_url function not working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get_absolute_url function not working?
#1
Hi,

So I am using django 2.2.7 and following videos on youtube for learning Python Django.

My models.py file consists of a class for an Album object and a class for a Song object.
Inside this models.py file I have declared a function called get_absolute_url (please see the attached image models).

I want to allow the user of the website to be able to create a new Album object by filling in a form and then clicking submit. Therefore, I have created this form - its consists of two separate codes shown in album_form.jpg and form-template.jpg. They just enter values for for input fields album, artist, genre and logo. Then they click submit. I have attached an image of what the actual forum looks like on the web.

However, the issue I am having is that clicking submit does not seem to do anything at all. The least I was expecting was to be redirected to the url written inside the get_absolute_url function which is "music:detail" passing in the primary key that gets created when new object of Album is made. The "music:detail" is the name for the url and it is path('<int:pk>/', views.DetailView.as_view(), name='detail')

Does anyone know why this is happening? Some people have mentioned changing the action in form code but the tutorials I am following leave the action as blank i.e. action="" so that's why I left it as blank. Not sure if that is the issue?

Thanks,

Attached Files

Thumbnail(s)
               
Reply
#2
I change my code to incorporate crispy_forms.
Anyways, this is what my final code looks like so far:

models.py:

from django.db import models
from django.urls import reverse

# Create your models here.
class Album(models.Model):
	artist = models.CharField(max_length=250)
	album_name = models.CharField(max_length=500)
	genre = models.CharField(max_length=100)
	album_logo = models.ImageField()

	def get_absolute_url(self):
		return reverse('music:detail', kwargs={'pk':self.pk})

	def __str__(self):
		return self.album_name + " - " + self.artist

class Song(models.Model):
	album = models.ForeignKey(Album, on_delete=models.CASCADE)
	file_type = models.CharField(max_length=100)
	song_title = models.CharField(max_length=250)
	is_favourite = models.BooleanField(default=False)

	def __str__(self):
		return self.song_title
views.py:

from django.views import generic
from .models import Album, Song


# Create your views here.
class IndexView(generic.ListView):
	template_name = 'music/index.html'
	queryset = Album.objects.all()
	context_object_name = 'all_albums'


class DetailView(generic.DetailView):
	model = Album
	template_name = 'music/detail.html'


class AlbumCreate(generic.CreateView):
	model = Album
	fields = ['artist', 'album_name', 'genre', 'album_logo']

	def form_valid(self, form):
		return super().form_valid(form)
album_form.html:

{% extends 'music/base.html' %}
{% load crispy_forms_tags %}

{% block title %}Add a New Album{% endblock %}
{% block body %}
	<form method="post">
		{% csrf_token %}
		{{ form|crispy }}
		<input type="submit" value="Add">
	</form>
{% endblock %}
The issue I am having now is that when I fill in the form and upload a .jpg image in the album_logo field,
after clicking the Add button, it gives me an error saying no file was uploaded for the album_logo field, this field is required. I don't know why it is complaining as this is not true, I did upload an image from my desktop into this field.
This is the form:

   
Reply
#3
i found the solution to my own problem.
i had to add enctype="multipart/form-data" in my form.
Reply
#4
Actually, since changing my form to the below, it now no longer works even though I have enctype="multipart/form-data"

{% extends 'music/base.html' %}

{% block title %}Add a New Album{% endblock %}
{% block body %}
	<form class="formContainer" method="post" enctype="multipart/form-data">
		{% csrf_token %}
		{% for field in form %}
			{% if field.label != 'Album logo' %}
				<label for="field{{ forloop.counter }}">{{ field.label }}</label>
				<input type="text" id="field{{ forloop.counter }}" name="" value="">
			{% else %}
				<label for="field{{ forloop.counter }}">{{ field.label }}</label>
				<input type="file" id="field{{ forloop.counter }}" name="" value="" accept="image/*">
			{% endif %}
			<br>
		{% endfor %}
		<input type="submit" id="submitBtn" name="" value="Add">
	</form>
{% endblock %}
Upon clicking the submit button in the form, it should take me to the details page for the Album that got created.
Does anyone have any idea please?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020