Javascript Django静态文件未加载或不工作?

Javascript Django静态文件未加载或不工作?,javascript,python,jquery,html,django,Javascript,Python,Jquery,Html,Django,我一直在尝试使用JQuery实现一种上传多个图像的方法。由于某些原因,当我单击上载照片按钮时,我的js文件没有被调用 每当我点击上传照片按钮,什么都不会发生 更新:我认为我的js文件没有加载,并且出现了浏览器控制台中显示的问题 我的代码如下: 我不确定我在视图或..中的create_posts功能是否有问题 views.py from django.shortcuts import render, redirect, get_object_or_404 from django.utils imp

我一直在尝试使用JQuery实现一种上传多个图像的方法。由于某些原因,当我单击上载照片按钮时,我的js文件没有被调用

每当我点击上传照片按钮,什么都不会发生

更新:我认为我的js文件没有加载,并且出现了浏览器控制台中显示的问题

我的代码如下:

我不确定我在视图或..中的create_posts功能是否有问题

views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from django.core.files.base import ContentFile
from django.forms import modelformset_factory
from django.http import JsonResponse

from .models import Projects, P_Images
from .forms import ProjectsForm, P_ImageForm
from PIL import Image
from io import BytesIO

# Create your views here.
# Function to create projects



def create_projects(request):
    #P_ImageFormSet = modelformset_factory(P_Images,form=P_ImageForm, extra=3)
    # Special case if the request method is not POST

    #if request.method == 'POST':
        project_form = ProjectsForm(request.POST, request.FILES)
        p_photos = P_ImageForm(request.POST, request.FILES)

        # Checks if the form is valid before save
        if project_form.is_valid() and p_formset.is_valid():
            instance = project_form.save(commit=False)
            instance.user = request.user
            instance.save()
            p_photos.save()
            data = {'is_valid': True, 'name': p_photos.file.name, 'url': p_photos.file.url}


        else:
            data = {'is_valid': False}
        #    print(project_form.errors, p_formset.errors)
        #return JsonResponse(data)
        context = { 
        'project_form': project_form,
        'p_photos': p_photos,
        }
        return render(request, 'projects/test2.html', context)


# Function to retrieve all different projects
def retrieve_projects(request):
    # Retrieves objects based on latest publish date
    projects_list = Projects.objects.all().order_by("-publish_date")
    context = {
        'projects_list': projects_list,
    }
    return render(request, 'projects/projects.html', context)

# Function to update projects
def update_projects(request, slug):
    instance = get_object_or_404(Projects, slug=slug)
    project_form = ProjectsForm(request.POST or None, instance=instance)

    # Update_date becomes the latest time
    if project_form.is_valid():
        project_form.save()
        return redirect('retrieve_projects')
    context = {
        'instance': instance,
        'project_form': project_form
    }
    return render(request, 'projects/forms.html', context)

# Function to delete projects chosen
def delete_projects(request, slug):
    Projects.objects.filter(slug=slug).delete()
    return redirect('retrieve_projects')

# Function to show details of project using the ids
def details_of_project(request, slug):
    # Will raise 404 if there is not such id
    project_details = get_object_or_404(Projects, slug=slug)
    context = {
        'project_details': project_details,
    }
    return render(request, 'projects/posts.html', context)
url.py

from django.urls import re_path, include
from . import views

# urls for projects page
app_name = 'p_photos'

urlpatterns = [
    re_path(r'^$', views.retrieve_projects, name="retrieve_projects"),
    re_path(r'^create/$', views.create_projects, name="create_projects"),
    re_path(r'^(?P<slug>[\w-]+)/$', views.details_of_project, name="details_of_project"),
    re_path(r'^(?P<slug>[\w-]+)/update/$', views.update_projects, name="update_projects"),
    re_path(r'^(?P<slug>[\w-]+)/delete/$', views.delete_projects, name="delete_projects"),
]
浏览器控制台错误

Loading failed for the <script> with source “http://localhost:8000/static/js/jquery-3.1.1.min.js”. create:20
Loading failed for the <script> with source “http://localhost:8000/static/js/bootstrap/bootstrap.min.js”. create:21
[Show/hide message details.] ReferenceError: jQuery is not defined jquery.ui.widget.js:18:5
[Show/hide message details.] TypeError: $ is undefined jquery.iframe-transport.js:49:5
[Show/hide message details.] TypeError: $ is undefined jquery.fileupload.js:38:5
[Show/hide message details.] ReferenceError: $ is not defined basic-upload.js:1:1
为具有源的加载失败”http://localhost:8000/static/js/jquery-3.1.1.min.js”。创建:20
加载源为“”的文件失败http://localhost:8000/static/js/bootstrap/bootstrap.min.js”. 创建:21
[显示/隐藏消息详细信息。]ReferenceError:jQuery未定义jQuery.ui.widget.js:18:5
[Show/hide message details.]TypeError:$未定义jquery.iframe transport.js:49:5
[Show/hide message details.]TypeError:$未定义jquery.fileupload.js:38:5
[显示/隐藏消息详细信息。]ReferenceError:$未定义基本上载。js:1:1

删除静态\u根目录,只有在收集静态文件时才应使用此选项。还要确保javascript位于名为static的文件夹中

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

在您的设置.py中调试设置为true吗?@Taylor是的。请确保已清除缓存并打开浏览器控制台。如果您的javascript中有任何错误,它可能会出现在那里。@Ramtin我检查了浏览器控制台,似乎在加载我的js文件时出现了问题。我不知道为什么有些东西没有定义。谢谢你的帮助@拉姆丁,我修好了!谢谢你的帮助!我终于修好了!谢谢你的帮助!
$(function () {
    /* 1. OPEN THE FILE EXPLORER WINDOW */
    $(".js-upload-photos").click(function () {
      $("#fileupload").click();
    });

    /* 2. INITIALIZE THE FILE UPLOAD COMPONENT */
    $("#fileupload").fileupload({
      dataType: 'json',
      done: function (e, data) {  /* 3. PROCESS THE RESPONSE FROM THE SERVER */
        if (data.result.is_valid) {
          $("#gallery tbody").prepend(
            "<tr><td><a href='" + data.result.url + "'>" + data.result.name + "</a></td></tr>"
          )
        }
      }
    });
  });
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '..', 'static'),
)
Loading failed for the <script> with source “http://localhost:8000/static/js/jquery-3.1.1.min.js”. create:20
Loading failed for the <script> with source “http://localhost:8000/static/js/bootstrap/bootstrap.min.js”. create:21
[Show/hide message details.] ReferenceError: jQuery is not defined jquery.ui.widget.js:18:5
[Show/hide message details.] TypeError: $ is undefined jquery.iframe-transport.js:49:5
[Show/hide message details.] TypeError: $ is undefined jquery.fileupload.js:38:5
[Show/hide message details.] ReferenceError: $ is not defined basic-upload.js:1:1
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')