connectting to mongodb with django pymongo

Hello All
I am trying to code a flashcard app that connects to mongodb and displays flashcard data on a web page from mongodb. I tested the connection last week by sending data to mongodb this worked so the connection seems to be fine however my code to show the flashcards in a web page from the server does work. I post some of the code below

views.py

‘’’ from django.views.generic import ListView ‘’’
‘’’ from django.shortcuts import render’‘’
‘’’ from .models import Card, myapp_collection’‘’
‘’’ from django.http import HttpResponse’‘’

Create your views here.

‘’‘def index(request):’‘’
‘’’ return HttpResponse(“

App is running

”)‘’’

‘’‘class CardListView(ListView):’‘’
‘’‘model = Card’‘’
‘’‘queryset = Card.objects.all().order_by(“box”, “-date_created”)[:5]’‘’
‘’'template_name = “card_list.html”

html file

‘’‘ ’‘’

‘’‘ ’‘’

‘’‘’’
‘’‘Flashcards ’‘’
‘’ ‘’
‘’‘’’

‘’‘’‘’

‘’‘’‘’
‘’‘’‘’
‘’‘

Flashcards App

’‘’
‘’’ ‘’’
‘’‘’‘’
‘’’ {% block content %} ‘’’
‘’’

Welcome to your Language Flashcards app!

‘’’
‘’‘{% endblock content %} ’‘’
‘’’ ‘’’
‘’‘’‘’

‘’‘’‘’

Note: When posting code, templates, or error messages and tracebacks here, enclose the code between lines of three backtick - ` characters. This means you’ll have one line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

It’s most helpful if you put the file contents between one set of ```. Don’t do this for individual lines.

The lines of ```, must be lines by themselves and not parts of other lines. Additionally, you’ll want to ensure that you are using the backtick - ` and not the apostrophe - '.

Please fix your post, otherwise we will be unable to read your post to offer suggestions.

Hello All
I am trying to code a flashcard app that connects to mongodb and displays flashcard data on a web page from mongodb. I tested the connection last week by sending data to mongodb this worked so the connection seems to be fine however my code to show the flashcards in a web page from the server does work. I post some of the code below
views.py

from django.views.generic import ListView
from django.shortcuts import render
from .models import Card, myapp_collection
from django.http import HttpResponse






# Create your views here.

def index(request):
    return HttpResponse("<h1>App is running</h1>")



class CardListView(ListView):
    model = Card
    queryset = Card.objects.all().order_by("box", "-date_created")[:5]
    template_name = "card_list.html"

    


def card_list(request):
    cards = Card.objects.all()  # Retrieve all cards from MongoDB
    return render(request, 'card_list.html', {'cards': cards})



#
# def get_all_myapp(request):
 #   myapp = myapp_collection.find()
 #   return myapp

#class CardListView(ListView):
#    model = Card
 ##   queryset = Card.objects.all().order_by("box", "-date_created")
  #  template_name = "card_list.html"

html file

<!-- cards/templates/cards/card_list.html -->

{% extends "myapp/base.html" %}

<h1>Card List</h1>
<ul>
{% for card in object_list %}
    <li>{{ card.your_field_name }}</li>  <!-- Make sure 'your_field_name' is a valid field of the Card model -->
{% endfor %}
</ul>

models.py



# Import necessary modules
from django.db import models
from . import db_con

# Access the 'myapp' collection from the database using db_con
myapp_collection = db_con.db['myapp']



class Card(models.Model):
    BOX_CHOICES = (
        ('A', 'Box A'),
        ('B', 'Box B'),
        ('C', 'Box C'),
    )

    # Fields for the Card model
    front_text = models.CharField(max_length=255)
    back_text = models.CharField(max_length=255)
    box = models.CharField(max_length=1, choices=BOX_CHOICES)
    date_created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Card ({self.id}) in {self.get_box_display()}"

So fare as I am aware the mongodb connection seems to be working I just cannot get the flashcards to appear in the web page the file type is csv.

Which view are you running?

  • What url is being invoked?
  • What’s its definition in your urls.py?

The view.py posted is the only view that I have for the app. the url being invoked should be cardlist.html. below I have posted the urls.py for the application and for the app.

urls.py for application

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

urls.py for myapp



from django.urls import path, include
from . import views
from django.views.generic import TemplateView
from .views import CardListView

urlpatterns = [
    path('', views.CardListView.as_view(), name='card_list'),  # URL for card list view (root)
   # path('', include('myapp.urls')),
]


models.py

from django.views.generic import ListView
from django.shortcuts import render
from .models import Card, myapp_collection
from django.http import HttpResponse






# Create your views here.

def index(request):
    return HttpResponse("<h1>App is running</h1>")



class CardListView(ListView):
    model = Card
    queryset = Card.objects.all().order_by("box", "-date_created")[:5]
    template_name = "card_list.html"

    


def card_list(request):
    cards = Card.objects.all()  # Retrieve all cards from MongoDB
    return render(request, 'card_list.html', {'cards': cards})



#
# def get_all_myapp(request):
 #   myapp = myapp_collection.find()
 #   return myapp

#class CardListView(ListView):
#    model = Card
 ##   queryset = Card.objects.all().order_by("box", "-date_created")
  #  template_name = "card_list.html"

You are not showing that you have a url defined for “cardlist.html”

Your template shows:

But you do not have a field named your_field_name in your Card model, so there’s nothing to render here.

So far as I can see I have cardListView as the url to be invoked is it written wrong?

Have you worked your way through the Official Django Tutorial? If not, you should - that’s the best place to get started with understanding the relationships between URLs and views.

If you have, then I suggest you review the work you did on part 1 and part 3 regarding URL configuration, along with the URL docs at URL dispatcher | Django documentation | Django and django.urls functions for use in URLconfs | Django documentation | Django to understand how URLs relate to views.