I have Jango in python Anyhere my view that generates pdf works locally but on the error server!

Asked

Viewed 54 times

1

Hello, I have a system in Django/python very simple to manage sweepstakes, and last xhtml2pdf to generate reports in pdf, when ultilizo it locally it works perfectly did the deploy in python Anywhere the whole system works normal except the view that generates the pdf with the data!! use of Django 3.1 I put this question in the stack a few minutes ago and was rejected due to lack of details, I am beginner, detailed questions require a more professional level. if you help me I try to show my problem!

ESSE É O ERRO QUE RECEBO NO TEMPLATE

THAT’S THE VIEW CODE

def render_pdf_view(request):
hoje = date.today()
dias = ('Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado', 'Domingo')
hj = dias[hoje.weekday()]
hora_agora = datetime.now()
hora = hora_agora.strftime('%H:%M')
acertivos = Escolha.objects.filter(sorteado=True).count()
pessoa = Pessoa.objects.all()

template_path = 'pdf_template/invoice.html'
context = {
    'hj':hj,
    'hora':hora,
    'hoje':hoje,
    'pessoa': pessoa,
    'acertivos':acertivos,
}
# Create a Django response object, and specify content_type as pdf
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format(hoje)
# find the template and render it.
template = get_template(template_path)
html = template.render(context)

# create a pdf
pisa_status = pisa.CreatePDF(
   html, dest=response )
# if error then show some funy view
if pisa_status.err:
   return HttpResponse('We had some errors <pre>' + html + '</pre>')
return response

1 answer

0

This error is not a code error, possibly the ports you are using on the server are blocking for some reason. Try running through a different door:

Adds to the py Settings.:

ALLOWED_HOSTS = ['*']

and then you do python Manage.py runserver 0.0.0.0:9191 [or any other door]

  • If I may, how can I do this directly on the server?

  • what ports are you running the service on? and what server are you using?

  • I only created the application that basically manage draw numbers, nothing special, but I want to generate reports in pdf, so I have this view that then takes the model data(name, id, hits and etc) and play everything in a pdf, but the port I use locally is the standard of Django :8000 so I just climbed it to python.

  • I’ve updated the answer, try this

  • should I run python Manage.... on python bash Anywhere? sorry if I’m being ignorant but the door is not set automatically so I make the deploy? This pdf generating scheme works perfectly in the local environment!

  • yes I know it works all locally , but sometimes when trying to implement things do not go as we expect. Your error is not due to code, but where it is implemented. By trying to change the doors where your program is running, you may resolve the issue, but another solution [link] (https://www.pythonanywhere.com/forums/topic/3485/) as described in this forum , python Anywhere , uses a proxy to filter records ,( for non-paying users), and may be blocked there

  • show dude, I will follow what the link you sent me asks to do! thank you so much for the help!! quaso certo posto aqui!!

  • If it works just upvote my comment for people with the same problem see the solution

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.