0

I apologize if this is obvious, but I'm relatively new to Django and couldn't find any resources for this.

Goal

I would like to create unique invoices that are stored on my server (or a CDN) when a form is filled out.

Question

Would it be possible to use Django forms to create an HTML file with a unique URL (or upload the newly created file to AWS).

I understand how to upload files, I just can't quite figure out how to create a new file using a form.

Update

The reason why I want to create a unique file is so that I can email the invoice to the customer so that they can view the invoice at a later point.

John R Perry
  • 3,916
  • 2
  • 38
  • 62
  • Possible duplicate of [Django - how to create a file and save it to a model's FileField?](https://stackoverflow.com/questions/7514964/django-how-to-create-a-file-and-save-it-to-a-models-filefield) – John R Perry Sep 30 '19 at 18:42
  • Are you sure that you want to create a unique HTML file for each invoice? Why not just have a unique URL and generate each invoice from a template? – Matthew Gaiser Sep 30 '19 at 18:43
  • yes you can use django forms for generating html. Use the model which has the field for filefeild for upload. - and config the upload location for AWS - I have done something of this sort for image upload using pyuploadcare. for AWS S3 you can check https://stackoverflow.com/questions/45418538/python-upload-file-through-aws-api-gateway-to-s3 – Bikiran Das Sep 30 '19 at 18:50

2 Answers2

2

Given that you are seeking a unique HTML file, I am assuming that you want to display the invoice on the web and not in some downloadable file. Why not do something like this? It doesn't create a new HTML file each time an invoice is created, but rather uses a template to generate the invoice for each person.

In your urls.py:

    path('invoice/<id:invoice_id>/', views.invoice, name='invoice'),

In your views.py:

def invoice(request, invoice_id):
    #I assume here that you are using the default form setup and saving the form to a model
    the_invoice = get_object_or_404(Invoice, invoice_id=invoice_id)
    return render(request, 'invoice_template.html', {'the_invoice': the_invoice,})

Then anytime you enter mysite.com/invoice/12432 it will take the 12432, assign it to the argument invoice_id and send that argument to your view function, as you can see. The invoice_id would be used to figure out which invoice was wanted and would return that invoice and its data to the template.

Does this suit your needs? Anyone using the website will get that specific invoice. It just will be generated on request and not always exist on the server as a static file.

Matthew Gaiser
  • 4,558
  • 1
  • 18
  • 35
  • 1
    Oh wowww this is a major palm to forehead moment. So, it's essentially like creating a blog post. I feel dumb now haha thank you so much! – John R Perry Sep 30 '19 at 21:00
  • I took a gamble that you didn't actually want to create html files but rather just wanted to show invoice information. Glad that I won and could be of help. – Matthew Gaiser Sep 30 '19 at 21:10
0

You can handle the logic in your view where the form is called.

def my_view(request): if form.is_valid(): do_something_with_data()

I would be more specific but it's challenging when there is no code to reference.

Jay_jen
  • 44
  • 5