V
V
Vladislav Kalyuzhny2020-04-28 09:48:41
Django
Vladislav Kalyuzhny, 2020-04-28 09:48:41

Django 3. Feedback form with attachment file. What's wrong with the code?

Good day to all!
I have this mailer. It works fine, letters come. But, attached files do not come. Tell me what's wrong here? Thank you!

forms.py

class ContactForm(forms.Form):
    name = forms.CharField(required=False, max_length=150, help_text="Name")
    email = forms.CharField(required=False, max_length=150, help_text="Email")
    file = forms.FileField(widget=forms.FileInput, required=False)
    message = forms.CharField(widget=forms.Textarea, help_text="Text area")


views.py
@csrf_exempt
def contact_us(request):
    if request.method == 'POST':
        form = ContactForm(request.POST, request.FILES)
        if form.is_valid():
            sender_name = form.cleaned_data['name']
            sender_email = form.cleaned_data['email']
            sender_file = form.request.FILES['file']
            message = "{0} New massege:\n\n{1}\n\n{2}\n\n{3}".format(sender_name,
                                                                           sender_email,
                                                                           form.cleaned_data['message'],
                                                                           sender_file,)
            send_mail('Subject', message, sender_email, ['[email protected]'])
            return render(request, 'pages/thank-you.html')
    else:
        form = ContactForm()

    return render(request, 'flatpages/report.html', {'form': form})


HTML
<div class="col-12 col-md-6">
            <div class="report">
                <h3 class="report-title">Contact Form</h3>
                <form method="post" action="/pro-upravlinnya/report/">
                    <div style="display:none">
                        <input type="hidden" name="csrfmiddlewaretoken" value="$csrf_token"/>
                    </div>
                    <div class="report-control">
                        {{ form.name.help_text }}
                        {{ form.name }}
                    </div>
                    <div class="report-control">
                        {{ form.email.help_text }}
                        {{ form.email }}
                    </div>
                    {{ form.file }}
                    <div class="report-control">
                        {{ form.message.help_text }}
                        {{ form.message }}
                    </div>
                    <div class="report-btn-wrp">
                        <button type="submit" class="report-submit" >Send</button>
                    </div>
                </form>
            </div>
        </div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-04-28
@bacon

1. https://docs.djangoproject.com/en/3.0/topics/http/... just read carefully
2 why csrf_exempt and at the same time ?

<input type="hidden" name="csrfmiddlewaretoken" value="$csrf_token"/>
yes, it's pointless. Just right<div style="display:none">{% csrf_token %}

E
elyasa, 2020-04-28
@elyasa

try:
    player_create_form = PlayerForm(request.POST, request.FILES)
    if player_create_form.is_valid:
      new_player = player_create_form.save(commit=False)
      new_player.player_name = player_name
      new_player.player_display_name = player_name
      new_player.player_fb_user_id = player_fb_user_id
      new_player.player_custom_avatar = player_fb_user_id
      new_player.player_settings = default_settings
                        new_player.player_avatar = request.FILES('player_avatar')
      new_player.save()
                        # Additional functions HERE
      pass
    else:
      return bad_response("player_create_form is not valid")
  except:
    return bad_response("Unknown error on player_create_form.save()")

  response = {
    "success": True,
    "player_id": new_player.id
  }

  return api_return_json_dumps(response)

This is in the basic sense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question