Javascript Can';不要让Django渲染模板
我试图让我的视图通过javascript的POST请求呈现我的HTML,但我无法使用标记的onclick=”“函数使其工作?我正在努力寻找答案,因为我不知道如何提出这个问题,所以我将尝试展示 我的观点是:Javascript Can';不要让Django渲染模板,javascript,python,django,Javascript,Python,Django,我试图让我的视图通过javascript的POST请求呈现我的HTML,但我无法使用标记的onclick=”“函数使其工作?我正在努力寻找答案,因为我不知道如何提出这个问题,所以我将尝试展示 我的观点是: def photos(request): BASE_DIR = Path(__file__).resolve().parent.parent workingDir = os.path.join(BASE_DIR, 'main', 'static', 'gallery', 'im
def photos(request):
BASE_DIR = Path(__file__).resolve().parent.parent
workingDir = os.path.join(BASE_DIR, 'main', 'static', 'gallery', 'img')
vehicle = request.POST.get('car') # Assigns the 'pass_vehicle' JS function returned value to 'vehicle' variable
print(request) # Debug
number_of_images = len(os.listdir(f'{workingDir}/{vehicle}/')) - 1 # Retrieve number of images within folder for <p> tag
cols = [(3, 6, 3), (8, 4), (6, 6), (4, 8)] # CSS columns used below for displaying images in specific layout on HTML page
htmlText = []
imgNum = 0
while imgNum <= 25: # During development limit number of pictures to display (will have specific amount in deployment)
for colNum in cols:
for x in range(len(colNum)): # Generate HTML to display each image within folder within the CSS column layout above
imgNum += 1
inputText = f'<div class="col-6 col-md-6 col-lg-{colNum[x]}" data-aos="fade-up">\n <a href="/main/static/gallery/img/{vehicle}/img_{imgNum}.jpg" class="d-block photo-item" data-fancybox="gallery">\n <img src="/main/static/gallery/img/{vehicle}/img_{imgNum}.jpg" alt="Image" class="img-fluid">\n <div class="photo-text-more">\n <span class="icon icon-search"></span>\n</div>\n</a>\n</div>'
htmlText.append(inputText)
context = {
'htmlText': htmlText # This returns the correct HTML and displays correctly IF 'href={% url 'photos' %}' in 'gallery.html'
}
## DIDN'T WORK EITHER
# t = loader.get_template('main/photos.html')
# return HttpResponse(t.render(context, request))
## This doesn't change the webpage but does console print the correct 'Debug'!
return render(request, 'main/photos.html', context)
我这样做的原因是为了让gallery页面从文件夹中动态更新,我可以使用相同的命名约定随时间添加新的文件夹/图像,并且网页应该自动更新
这一切都很完美,因为它从正确的目录返回照片并正确填充HTML代码,但它不会加载photos.HTML页面,除非我在标记中更改“href={%url'photos'%}”,但这样就不会发送帖子数据了?!我尝试了几种返回方法,如HttpResponse或redirect,但我的配置肯定是错误的
如果有人能理解这个问题并为我指出正确的方向,那将是一个很大的帮助 您可以更新以包含您的
模板设置吗?my settings.py模板设置为“TEMPLATES=[{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':[os.path.join(BASE_DIR,'main','TEMPLATES')],'APP DIRS':True,'OPTIONS':{'context\u processors':['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},},,],如果您指的是URL…urlpatterns=[path](“”,views.home,name='home')、path('services',views.services,name='services')、path('about',views.about,name='about')、path('contact',views.contact,name='contact')、path('gallery',views.gallery,name='gallery')、#相关路径('photos',views.photos,name='photos')、#相关]
{% for entry in titleDict %}
<div class="pd-top col-6 col-md-6 col-lg-4" data-aos="fade-up">
<a id="{{ entry.filename }}" href="#" class="d-block photo-item" onclick="pass_vehicle(this)"> <!--As 'id' is dynamic using JS to obtain id and forward to view -->
<img src="/main/static/gallery/img/{{ entry.filename }}/img_1.jpg" alt="Image" class="img-fluid">
<div class="photo-text-more">
<div class="photo-text-more">
<h3 class="heading">{{ entry.vehicle }}</h3>
<span class="meta">{{ entry.numOfPictures }} Pictures</span>
</div>
</div>
</a>
</div>
{% endfor %}
function pass_vehicle(tag){
$.ajax({
url: "photos",
type: "POST",
data :{ 'car' : tag.id },
success: callback
});
}
function callback(response) {
console.log(response)
}