Home > Blockchain >  how to use a media file in a django view, in other words, how do i get the address of the media file
how to use a media file in a django view, in other words, how do i get the address of the media file

Time:10-10

I have Included the following in my settings.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

This is the model whose FileInput i have to store:

class Clip(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=False)
    audio = models.FileField(upload_to='audio/')

    class Meta:
        db_table = 'Audio_store'

My modelForm:

class AudioForm(forms.ModelForm):
    class Meta:
        model = Clip
        fields = ['audio']

And finally the view where i need the address of this file:

        form = AudioForm(request.POST, request.FILES or None)
        try:
            instance = Clip.objects.get(owner=request.user)
            instance.delete()
        except Clip.DoesNotExist:
            instance = 0
        if form.is_valid():
            instance = form.save()
        else:
            return HttpResponse("Failed")
        result = function_name("""destination of the audio file in media directory""")

Now as you can see to get the value of result, i need to feed the address of the file to the function. Now i am confused what should i put in the function.

i have a media directory in which i have created a subdirectory 'audio'. But i can't figure out how do i put in the exact address with the file name to this function. (This is the only way i can use this function)

Please help me out here

Edit: This is how i am taking user input:

<form method="POST" action="{% url 'application:audio' %}">
                        {% csrf_token %}
                        {{ form | crispy }}
                        <br>
                        {% if form.errors %}
                      {% for field in form %}
                          {% for error in field.errors %}
                              <div class="alert alert-danger">
                                  <strong>{{ error|escape }}</strong>
                              </div>
                          {% endfor %}
                      {% endfor %}
                      {% for error in form.non_field_errors %}
                          <div class="alert alert-danger">
                              <strong>{{ error|escape }}</strong>
                          </div>
                      {% endfor %}
                  {% endif %}
                  <br>
                        <input type="submit" class="btn btn-primary mt-3 mb-3 btn-register" value="Upload and Connect!!" />
                      </form>

The form.errors display nothing, but the form is never validated. I am uploading an mp3 file via the form

Thank You

CodePudding user response:

You can obtain the path of the audio of the instance with the .path attribute [Django-doc]:

instance.audio.path

if you want the media URL, you can use the .url attribute [Django-doc]:

instance.audio.url

You probably should first check if the instance is not 0, since in that case, you do not work with a Clip model.

In your form, you should specify the enctype="…" HTML attribute [dev-mozilla] in the <form> tag:

<form method="POST" action="{% url 'application:audio' %}" enctype="multipart/form-data"></form>
  • Related