Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 5 months ago by VenusianDiscoverer638

How do I create a Django view to check for drug interactions based on user input?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm building a Django application that checks for potential interactions between drugs entered by the user.

My interface includes an HTML form where users can add drugs and click a button to see results. If interactions are found, the results should be highlighted (e.g., with red flag text).

I have defined a model for storing interactions as follows:

PYTHON
class Interaction(models.Model): drug1 = models.CharField(max_length=100,null=True, blank=True)#alternatives drug2 = models.CharField(max_length=1500,null=True, blank=True) interaction = models.CharField(max_length=1300,null=True, blank=True) note = models.CharField(max_length=1000,null=True, blank=True)

The HTML template is structured like this:

HTML
<body> <div id="app"> <h1 class="text-center my-5">Drug-Drug Interactions Tool</h1> <h5 class="text-center">Added Drugs:&nbsp</h5> <p id="drugCollection" class="text-center">No drugs added yet</p> <div class="container"> <div class="form-row"> <div class="input-group input-group-lg col-md-3 mx-auto"> <input class="form-control" type="text" placeholder="Enter medication here" id="txtMedication" onkeydown="keypress()"> </div> </div> <div class="col text-center"> <button type="button" class="btn btn-primary my-2" onclick="addMedication()" >Add drug</button> </div> <div class="col text-center"> <button type="button" class="btn btn-primary my-2" onclick="submitMediactions()">See the Result</button> </div> </div> {% if interactions is defined %} <div class="col text-center"> <h2 class="mt-5">Results:</h2> {% if interactions|length != 0 %} {% for interaction in interactions %} <hr> <p><b>{{interaction.object}}</b> and <b>{{interaction.precipitant}}</b>{% if interaction.description|length != 0 %}: {{interaction.description}}{% endif %}</p> {% endfor %} {% else %} <p>No Interactions could be found.</p> {% endif %} </div> {% endif %} <div></div> </div> </body> {% endblock %}

The client-side JavaScript managing the drug inputs is as follows:

JAVASCRIPT
var medications = [] function keypress(){ if(event.key === 'Enter'){ addMedication() } } function addMedication(){ textBoxVal = document.getElementById('txtMedication').value; if(textBoxVal != ""){ medications.push(textBoxVal); document.getElementById('drugCollection').innerHTML = medications; document.getElementById('txtMedication').value = "" console.log("Medication added:", textBoxVal); } } function submitMediactions(){ window.location.href = "/?medications=" + medications.toString(); //'/indexnterctiondrug' }

I need help constructing a Django view that processes the medications passed (e.g., via GET parameters) and returns the HTML template with the appropriate interaction results. What is the best approach to implement this view?

0

Comments 0

Answers 0

Views 48

0 Answers

Discussion

No comments yet.