-1

I am working on my first Django project and learning a lot along the way. So far, I have set up a very simple website and am having trouble with the next steps. Through this project, I am hoping to achieve a website that provides users with reliable resources based on how they answer the three questions they are asked.

Website Explanation: The user opens the website and is prompted to click a button to go to the first step. The first step asks the user to choose the subject they would like a resource on (for now, all of the listed subjects are related to cooking). Then, they choose their level of expertise (beginner, intermediate, advanced). Finally, they choose a preferred medium for their resource (article/blog, video, book). Based on the subject, their level of expertise, and their preferred medium, the last page will give the user a resource that is retrieved from a linked database.

Where I Am Stuck: So far, I have created five pages (home page, one page for each question, and a results page) and have set up the choices for each question. Now, I am having trouble finding a way to combine the user's three answers into one variable, which I will use to fetch data from a connected database and display the respective resources.

For reference, I have included code from my "views.py" file and "Subject Page" file (this page is Step 1 of the process where the user chooses a subject. The format for the other two questions is very similar to this one, which is why I only included the code for this page). If you are willing to help, please feel free to request any other references I may be able to provide. Also, though I am not yet at that step, I'm open to guidance on connecting my website to a database. As always, any help/guidance would be much appreciated! Thank you :)

views.py code:

from django.shortcuts import render
from django.http import HttpResponse

def HomePage(request):
    return render(request, 'ResourceApp/HomePage.html')

def SubjectPage(request):
    return render(request, 'ResourceApp/SubjectPage.html')

def LevelPage(request):
    return render(request, 'ResourceApp/LevelPage.html')

def MediumPage(request):
    return render(request, 'ResourceApp/MediumPage.html')

def ResultsPage(request):
    return render(request, 'ResourceApp/ResultsPage.html')

Subject Page code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Subject | Tools For Fools</title>
</head>
<body>

<h1> Step 1 </h1>
<form action="http://127.0.0.1:8000/level/">
  <label for="Subject">Choose a Subject:</label>
  <select id="Subject" name="Subject">
    <option value="Knife Skills">Knife Skills</option>
    <option value="Culinary Terms">Culinary Terms</option>
    <option value="Recipes">Recipes</option>
  </select>
  <input type="Submit">
</form>

</body>
</html>

Error

views.py file urls.py file Cookies

2 Answers2

0

In your urls.py include:

urlpatterns = [
   path('level/',views.LevelPage,name='levelpage'),
   path('medium/',views.MediumPage,name='mediumpage'),
   path('results/',views.ResultsPage,name='rpage'),

]

change your views.py to:

from django.shortcuts import render
from django.http import HttpResponse

def HomePage(request):
    return render(request, 'ResourceApp/HomePage.html')

def SubjectPage(request):
    return render(request, 'ResourceApp/SubjectPage.html')
def LevelPage(request):
    subject = request.GET.get('Subject')
    response = render(request, 'ResourceApp/LevelPage.html')
    response.set_cookie('subject',subject)
    return  response

def MediumPage(request):
    level = request.GET.get('level')
    response = render(request, 'ResourceApp/MediumPage.html')
    response.set_cookie('level',level)
    return  response


def ResultsPage(request):
    medium = request.GET.get('medium')
    results = request.COOKIES.get('subject') + request.COOKIES.get('level') + request.COOKIES.get('medium')
    return render(request, 'ResourceApp/ResultsPage.html')
Talha Quddoos
  • 556
  • 6
  • 17
  • Thank you for your response! After following your suggestions, I ran into an error on my "results" page. The error says "TypeError at /results/ can only concatenate str (not "NoneType") to str". Any ideas on why this may be happening? – Rahat Choudery Jun 03 '20 at 21:57
  • @RahatChoudery, kindly send me a screenshot of your code and a screenshot of your error page. – Talha Quddoos Jun 04 '20 at 07:14
  • For some reason, it is not allowing me to add images to my comment. So, I went ahead and linked the images to the bottom of my original post. You can find them under "Error", "views.py file", and "urls.py file". Thank you in advance and please let me know if I can provide anything else! – Rahat Choudery Jun 04 '20 at 15:42
  • @RahatChoudery, show me COOKIES information shown in this picture. [View photo](http://talhaqqqq.000webhostapp.com/django-error-screen-shot.jpg) – Talha Quddoos Jun 04 '20 at 16:29
  • I've added the screenshot to the original post under "Cookies". Please let me know if you need any other screenshots! – Rahat Choudery Jun 04 '20 at 17:31
0

Well, you're not showing your models, but if your datamodel is sane, then just work with path components. If you use slugs, then that's also very good for SEO.

Basically, your user would travel this route:

/
/resources/knife-skills/
/resources/knife-skills/beginner/
/resources/knife-skills/beginner/blogs/

And your final model would be:

class Resource(models.Model):
    subject = models.ForeignKey(Subject)
    level = models.CharField(choices=LEVEL_CHOICES)
    medium = models.CharField(choices=MEDIUM_CHOICES)
    resource = models.URLField()

And the result page url (the longest one) would be:

path('<slug:subject>/<slug:level>/<slug:medium>/', results_page)

This flows naturally and doesn't rely on cookies, so the user can bookmark it and come back, search engines can link the pages, etc.