1

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

3
  • Can you please add your forms.py ? Commented Mar 4, 2020 at 12:15
  • In order to help with the question can you please show your template file. We need to know what you are passing into the view, how you are passing it in (get or post), if you are using a django form or plain HTML form and what you are then doing with the data. The view above suggests that you are using date_min and date_max but without seeing the template we can't tell. Your forms.py that you have posted is also not relevant to this question Commented Mar 4, 2020 at 13:46
  • i have added the template file. i guess thats exactly the part you need. Commented Mar 4, 2020 at 14:06

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.