I’ve got a Python script which counts and prints the number of words in a text file. The script runs beautifully. It takes a public domain book (a text file such as Alice and Wonderland) which then counts the top 10 most used words (but which also filters out stopwords). See here for some of my previous work: https://stackoverflow.com/questions/56436291/filtering-stop-words-out-of-a-large-text-file-using-package-nltk-corpus
Now I am trying to ‘port’ this Python shell script to Django. My intention for this project is to have the Django app count the number of words in a blog post. But for now I’m still using Alice and Wonderland in .txt format.
I’ve encountered some issues with my local dev server running but serving me a series of name errors involving my views module when I navigate to the url. I was hoping some of you could provide some insight.
When I run the server and navigate to http://127.0.0.1:8000/seth/, this is the error showing in my web browser pointing to the issue at hand: https://pastebin.com/52x2c4iN
Here is the traceback from my local Django dev server in my shell: https://pastebin.com/a8PTcRki
The file with the most problems is my counters/views.py (current app only): https://github.com/Angeles4four/CC_Redact_Iter2/blob/v1.2/counters/views.py
I realize that the problem is with the way the text variable is tossed around. I probably should not be invoking global variables as I do at lines 24, 38, 39 in my views model (above). Furthermore, out of the Udemy course material I’ve watched and in the official Django docs I’ve read, I’ve never seen a views.py which uses main() for calling functions. I realize I am sort of departing from Django norms here. What might you people recommend I try instead?
Here is counters/models.py:
https://github.com/Angeles4four/CC_Redact_Iter2/blob/v1.2/counters/models.py
counters/forms.py: https://github.com/Angeles4four/CC_Redact_Iter2/blob/v1.2/counters/forms.py
urls.py in parent project directory: https://github.com/Angeles4four/CC_Redact_Iter2/blob/master/CC_Redact_Iter2/urls.py
Here is Alice and Wonderland in .txt format: https://github.com/Angeles4four/CC_Redact_Iter2/blob/v1.2/counters/Alice.txt
What other comments might you have about my code, in particular the views.py linked to above?
I’m running Django 2.2 and Python 3.7 on Manjaro.
Pull requests on GitHub are welcome although I realize if asking for pull requests like this might not be reasonable for most of you but I thought I’d say as always, just in case a kind and generous forum contributor has time on their hands. Requirements.txt is included on the master branch.
For what it is worth, here is some of the documentation I’ve been working with.
I’ve referred to official Django docs for:
File uploads: https://docs.djangoproject.com/en/2.2/topics/http/file-uploads/
Form handling with class-based views: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-editing/
HttpRequest.FILES: https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpRequest.FILES
Stack Overflow questions I’ve used:
What does the if __name__ == "__main__": do? https://stackoverflow.com/questions/419163/what-does-if-name-main-do
reverse for success_url on Django Class Based View complain about circular import https://stackoverflow.com/questions/31275574/reverse-for-success-url-on-django-class-based-view-complain-about-circular-impor
How can I get uploaded text file in view through Django? https://stackoverflow.com/questions/39436612/how-can-i-get-uploaded-text-file-in-view-through-django
How can i get the file name from request.FILES? https://stackoverflow.com/questions/3111779/how-can-i-get-the-file-name-from-request-files
How to reverse_lazy to a view/url with variable? https://stackoverflow.com/questions/46184193/how-to-reverse-lazy-to-a-view-url-with-variable