Python Django链接URL

问题描述 投票:0回答:1

我正在从事在线学校项目,我只是将学科与课程联系在一起,因此,当您单击课程时,您会看到它的学科页面,并且在学科页面中有一些课程(课程使用ID而不是ug头),我的问题是在HTML页面中链接课程URL时

所以我的问题是如何将课程链接到HTML页面中的材料(主题)?

我的代码:

HTML页面:

<!DOCTYPE html>
<html>
    <head>    
        <meta charset="utf-8">
        <title>Name</title>
        <link rel="stylesheet" href="/static/css/style.css">
    </head>
    <body>
        <div>
            <nav>
                <div class="logo"><img src="/static/images/Logo.png" width=50px></div>
                <ul class="navul">
                    <li class="navli"><a class="nava" href="404.html">حول الموقع</a></li>
                    <li class="navli"><a class="nava" href="404.html">المكتبة</a></li>
                    <li class="navli"><a class="nava" href="404.html">الدورات</a></li>
                    <li class="navli"><a class="nava" href="/classes">الصفوف</a></li>
                    <li class="navli"><a class="nava" href="/">الصفحة الرئيسية</a></li>


                </ul>
            </nav>

            <div class="div1">
                <img src="/static/images/Logo.png" width="90" class="logo2">
                <h1 class="t1">المواد </h1>
            </div>

            <div class="cardrow">
                {% for material in material.all %}

                <div class="cardcolumn">

            {% for Lesson in Materials.vedio_set.all %}
            <a href="{% url 'vedio' lesson.id %}" > lesson1</a>
            {% endfor %}


                    <div class="card">
                    <img class="imgcls" src="{{ material.image.url }}">
                    <h1>{{ material.title }}</h1>
                    </div>
                    </a>
                   {% endfor %} 
                </div>              
            </div>
        </div>
    </body>
</html>

MODELS.py:

from django.db import models
from users.models import *


# Create your models here.
class Class(models.Model):
    image= models.ImageField(upload_to="images")
    name= models.CharField(max_length=200, default=1)
    title= models.CharField(max_length=200)

    def __str__(self):
        return self.title

class Material(models.Model):
    name= models.CharField(max_length=200, default=1)
    title= models.CharField(max_length=200)
    classes= models.ForeignKey(Class, default=1, on_delete=models.SET_DEFAULT)

    def __str__(self):
        return self.name
class Lesson(models.Model):
    slug=models.SlugField()
    video=models.FileField(upload_to="videos", default="")
    title=models.CharField(max_length=200)
    description=models.TextField()
    thumbnail=models.ImageField(upload_to="images")
    Material=models.ForeignKey(Material, default=1, on_delete=models.SET_DEFAULT)
    allowed_membrship=models.ManyToManyField(Membership)
    def __str__(self):
        return self.title

VIWES.py:

from django.shortcuts import render, get_object_or_404
from .models import Class, Material, Lesson
# Create your views here.


def home(request):
    Classes= Class.objects
    return render(request, 'classes/index.html', {"class":Classes})

def classes(request):
    Classes= Class.objects
    return render(request, 'classes/Classes.html', {"class":Classes})

def materials(request, classes_id):
    theclass= get_object_or_404(Class, pk=classes_id)
    Materials = Material.objects.all()
    context = {
        'classes': theclass,
        'material': Materials
    }
    return render(request, 'classes/Materials.html', context)


def vedio(request, lesson_id):
    theMaterial=get_object_or_404(Material, pk=lesson_id)
    theLesson=Lessons.objects.all
    context={
        'Material': theMaterial,
        'Lesson':theLesson
    }
return render(request, 'classes/Lesson.html', context)

URLS:

 from django.contrib import admin
    from django.urls import path
    import classrooms.views
    import users.views
    from django.conf import settings
    from django.conf.urls.static import static

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', classrooms.views.home),
        path('classes', classrooms.views.classes),
        path('classes/<int:classes_id>', classrooms.views.materials, name="material"),
        path('signup', users.views.signup, name="signup"),
        path('login', users.views.login, name="login"),
        path('logout', users.views.logout, name="logout"),
        path('class/<int:classes_id>/<int:lessson_id>', classrooms.views.vedio, name="lessons")
    ]

    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我认为我遇到了一些典型的错误,对不起

成员身份在另一个应用程序中,我认为您不需要它。就是这样,希望我明白了,谢谢

python django url
1个回答
-1
投票

Materials.html中,您可以通过Material.Lessons_set.all()进行反向访问:

© www.soinside.com 2019 - 2024. All rights reserved.