我有一个正在多个浏览器上测试的站点。页面会根据您选择的城市而变化。一切正常。它更改内容,背景图像等。它还更改本地存储和会话存储上的会话数据。有一些值可以通过我的views.py使用此会话数据来提供特定数据(例如货币,价格等),而我当然不使用javascript来提供这些数据,这与其他更改(例如背景图片等)不同。 Chrome,Firefox和Opera。但是,不能可以在IE和Edge上使用。我必须使用F5手动刷新页面,以使会话更改最终发生,而javascript更改立即发生。但是我需要为最终用户提供一切自动化的工具。
我已经尝试使用window.location.reload(true)并在单击/选择标题后在各个点上使用了它的变体,但到目前为止尚未奏效。
有什么想法吗?我添加了JavaScript并查看了相关问题。
JavaScript
更改其他属性,例如背景图片和重新加载
$(".header-city-option").click(function(ev){
ev.preventDefault();
var city = $(this).data("name");
var id = $(this).data("id");
localStorage.setItem("city-name", city);
localStorage.setItem("city-id", id);
$("#header-city").html(city + ' <span class="caret"></span>');
$.each( $(".city-filter-input") , function(i, v){
$(v).val(id);
});
setCityInServer(city, id, true);
reloadpageFunction(true);
window.location.reload(true);
});
function changeHomepageByCity(city, id, isClicked){
changeBackground(city);
changeText(city);
setCityInServer(city, id, isClicked);
reloadpageFunction(isClicked);
}
function reloadpageFunction(isClicked) {
if(isClicked)
window.location.reload(true);
}
function changeBackground(city){
var urlBackground;
switch(city){
case "Manchester":
urlBackground = "https://blahblahblah";
break;
case "Liverpool":
urlBackground = "https://s3-eu-";
break;
case "London":
urlBackground = "https://s3-eu-";
break;
case "Madrid":
urlBackground = "https://s3-eu-west-1.";
break;
case "Barcelona":
urlBackground = "https://s3-eu-west-";
break;
case "Paris":
urlBackground = "https://s3-eu-west-";
break;
}
$("#main").css("background-image", "url(" + urlBackground + ")");
}
function changeText(city){
switch(city){
case "Manchester":
$("#main h2").html("Live the languages with Milingual in Manchester");
$("#main p").html("We organize language conversation groups in the best venues in Manchester");
break;
case "Liverpool":
$("#main h2").html("Live the languages with Milingual in Liverpool");
$("#main p").html("We organize language conversation groups in the best venues in Liverpool");
break;
case "London":
$("#main h2").html("Live the languages with Milingual in London");
$("#main p").html("We organize language conversation groups in the best venues in London");
break;
case "Madrid":
$("#main h2").html("Live the languages with Milingual in Madrid");
$("#main p").html("We organize language conversation groups in the best venues in Madrid");
break;
case "Barcelona":
$("#main h2").html("Live the languages with Milingual in Barcelona");
$("#main p").html("We organize language conversation groups in the best venues in Barcelona");
break;
case "Paris":
$("#main h2").html("Live the languages with Milingual in Paris");
$("#main p").html("We organize language conversation groups in the best venues in Paris");
break;
}
}
function setCityInServer(city, id, isClicked){
var token = $("input[name*='csrfmiddlewaretoken']")[0].value;
if(isClicked)
window.location.reload(true);
$.ajax({
type: "POST",
url: "set-city-session",
data: {
cityName: city,
cityId: id,
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function(){
if(isClicked)
window.location.reload(true);
}
});
}
View.py
例如,除非手动刷新,否则city和currency_qs变量不会更改。
def IndexPage(request):
if 'city-name' not in request.session:
cityName='Madrid'
request.session['city-name'] = request.POST.get('cityName')
else:
cityName = request.session['city-name']
if 'city-id' not in request.session:
cityId = 1
request.session['city-id'] = request.POST.get('cityId')
else:
cityId = request.session['city-id']
city = get_object_or_404(City, name=cityName)
servercurrency_qs = City.objects.filter(name=cityName).values('currency')
currency_qs = '€'
for currency in servercurrency_qs:
if currency['currency'] == 1:
currency_qs = '€'
elif currency['currency'] == 2:
currency_qs = '£'
else:
currency_qs = '$'
venues = ProfileVenue.objects.all().filter()
oc_list = []
for venue in venues:
if Occurrence.objects.filter(date__gte=timezone.now()).filter(event__location=venue).exists() and venue.city.name==cityName:
oc = Occurrence.objects.all().filter(date__gte=timezone.now()).filter(event__location=venue)[:1].get()
'''
venue_rating_qs = OccurrenceRating.objects.filter(occurrence__event__location=venue).aggregate(Avg('vn_rating'))
venue_rating=venue_rating_qs.get("vn_rating__avg","")
oc.event.location.rating=venue_rating
'''
oc_list.append(oc)
if len(oc_list) == 3: break
teachers = ProfileTeacher.objects.all().filter().filter(active=True).filter(published=True).filter(city__name=cityName)[:3]
teachers_list = []
for teacher in teachers:
if teacher.city.name==cityName:
"""
teacher_rating_qs = OccurrenceRating.objects.filter(occurrence__teacher=teacher).aggregate(Avg('tc_rating'))
teacher_rating=teacher_rating_qs.get("tc_rating__avg","")
"""
teachers_list.append(teacher)
languages = Language.objects.all()
levels = LanguageLevel.objects.all()
events = EventType.objects.all()
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
whoareyou = form.cleaned_data['whoareyou']
contact_name = form.cleaned_data['name']
phone_number = form.cleaned_data['phone_number']
registered_mandrill_email = 'contactus@milingual.com'
template = get_template('contact_template.txt')
contextemail = Context({
'from_email':from_email,
'contact_name':contact_name,
'phone_number':phone_number,
'whoareyou':whoareyou,
'message':message,
})
content = template.render(contextemail)
try:
email= EmailMessage(subject,content,registered_mandrill_email,['info@milingual.com'],reply_to = [from_email],)
email.send()
except BadHeaderError:
return HttpResponse('Invalid header found.')
#return render(request,"emailsuccess.html")
return redirect("emailsuccess")
context = {
'venues_today': oc_list,
'teachers': teachers,
'languages': languages,
'levels': levels,
'events': events,
'form': form,
'currency_qs':currency_qs,
'servercurrency_qs':servercurrency_qs,
'currency': currency,
'city':city,
}
return render(request, "index.html", context )