Bhugs Hardy
Bhugs Hardy

Reputation: 19

How to pass variables from html file to any python file like file.py in Django?

I have a project in Django. And i have a form named report.html. This form has got fields that users enters date ranges. The form is shown below. This is a form that allows users to select date ranges

So the project has got a model named sentiment.py this model takes sentiments from the database and analyses them the comments are filtered with date ranges . Below is the code of the file `# Main function

    from sentiment_analysis.views import repos
     # Main function
def main():
    print('Loading the Classifier, please wait....')
    classifier = joblib.load('svmClassifier.pkl')
    print('READY')
    context = {
         'posts': Post.objects.filter(date_posted__gte=date(repos.date_max)).filter(date_posted__lte=date(repos.date_min))
     }
    comment=str(context)
    print(predict(comment, classifier))


if __name__ == '__main__':
    main()

    `

instead of using static values like in the code, I need to input a variable that contains values that users punch on the report.html form.

So i have tried taking the values from the views.py file shown below..

 `def repos(request):
    date_min = datetime.datetime.strptime(request.GET['date_min'], "%d-%m-%Y").date()
    date_max = datetime.datetime.strptime(request.GET['date_max'], "%d-%m-%Y").date()
    data = Post.objects.filter(date_posted__gte=date_main, date_posted__lte=date_max)
    return render(request, 'sentiment_analysis/report.html', {'data': data})

    `

so i have tried to call the variables datemax and datemin from sentiment.py after importing the views.py in sentiment.py

The template that is report.html contains this code..

<div class="card-body">
          <div class="chart-area">
            <form method="POST">
              {% csrf_token %}
              <fieldset class="form-group">
                <label for="datemax">Enter Start Date 2020-01-01:</label>
                <input type="date" id="datemax" name="datemax" max="2050-12-31"><br><br>
              
                <label for="datemin">Enter End date</label>
                <input type="date" id="datemin" name="datemin" min="2000-01-02"><br><br>
                
              </fieldset>
              <div class="form-group">
                  <button class="btn btn-primary btn-user btn-block" type="submit">Generate Report</button>
              </div>
          </form>
          </div>
        </div>
      </div>
    </div>

The code in forms.py is the one below..

    from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

Please help me guys on how i can achieve this

Upvotes: 1

Views: 341

Answers (1)

Bhugs Hardy
Bhugs Hardy

Reputation: 19

This has been solved by using the django_rest_framework. You may close the question.

Upvotes: 1

Related Questions