0

I need help getting my receipt printer working with post/get commands. Here is the code in django/python. It runs without any error message, but it's not printing anything

import requests
import json
from email import header
import mimetypes
import urllib.request
from urllib import response
from django.http import HttpResponse
from django.http import JsonResponse
content = "some content"

if request.method == "POST":
    data = {"jobReady": "true", "mediaTypes":["text/plain"]}
    return JsonResponse(data)

if request.method == "GET":
    data = open("print.txt","r") 

    response_r = {
        'response':data,
        'status':200,
        'mimetype':'text/plain'
    }
    return HttpResponse(response_r)
    
if request.method == "DELETE":
    status_code = request.GET['code']

    if (status_code == "200 OK"):
        print("Success")
        return HttpResponse(status=200)

    if (status_code == "211 OK Paper Low"):
        print("Success, Paper Low")
        return HttpResponse(status=200)

Here's the code in flask. It's working and printing properly, but I want to rewrite it in Django.

from email import header
import mimetypes
from urllib import response
import urllib.request
from flask import Flask, request, jsonify, Response, make_response

app = Flask(__name__)

#flask --app CloudPRNTDemo run -h 192.168.1.218 -p 8000
@app.route('/')
def home():
    yes = ("home page")
    return(yes)

@app.route('/print/', methods=['POST', 'GET', 'DELETE'])
def cloudPRNTPOST():
    if request.method == "POST":
        data = {"jobReady": "true", "mediaTypes":["text/plain"]}
        print(data)
        return jsonify(data)

    if request.method == "GET":
        data = open("print.txt","r")        
        response_r = app.response_class(
            response=data,
            status=200,
            mimetype='text/plain'
        )
        # response.headers['X-Star-Buzzerendpattern'] = 1
        return response_r
    if request.method == "DELETE":
        status_code = request.args.get('code')
        print(status_code)

        if (status_code == "200 OK"):
            print("Success")
            return app.response_class(status=200)

        if (status_code == "211 OK Paper Low"):
            print("Success, Paper Low")
            return app.response_class(status=200)

    return 
    

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug = True)

Can someone translate the code from flask to django?

stelity
  • 33
  • 8
  • what have you done so far to setup a basic django project structure? you need urlpattern in urls.py and your code in a view function in views.py – Razenstein Apr 10 '23 at 03:53
  • This is basically one function in views.py. The rest of the stuff like urls.py, settings, etc aren't relevant. This specific function is not a webpage, it listens and sends post/get/delete commands to/from the receipt printer. When the receipt printer receives a GET command from python, it prints out a receipt with the contents specified in the variable "data". – stelity Apr 10 '23 at 04:20

0 Answers0