Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为模板中的特定使用Django(类)视图(通过Ajax);我正在我的项目中集成django聊天室应用程序_Ajax_Django_Class_Templates_Chatroom - Fatal编程技术网

如何为模板中的特定使用Django(类)视图(通过Ajax);我正在我的项目中集成django聊天室应用程序

如何为模板中的特定使用Django(类)视图(通过Ajax);我正在我的项目中集成django聊天室应用程序,ajax,django,class,templates,chatroom,Ajax,Django,Class,Templates,Chatroom,我对Django比较陌生。我想将Django聊天室应用程序集成到我的项目中。 此应用程序有3个不同的URL 1.Available-ChatRooms list 2.Set-guestname/Login 3.Roomactual聊天界面。 但我想让所有这3个功能加入room,设置guestname/login和ChatWindow,让用户可以在我的项目主页的同一个URL上使用,所以我将通过Ajax来实现 我需要从应用程序调用3类视图;这是应用程序的相应URL和视图文件: url.py 包含Ho

我对Django比较陌生。我想将Django聊天室应用程序集成到我的项目中。 此应用程序有3个不同的URL 1.Available-ChatRooms list 2.Set-guestname/Login 3.Roomactual聊天界面。 但我想让所有这3个功能加入room,设置guestname/login和ChatWindow,让用户可以在我的项目主页的同一个URL上使用,所以我将通过Ajax来实现

我需要从应用程序调用3类视图;这是应用程序的相应URL和视图文件:

url.py 包含Home.html模板的My Home视图,我想在其中集成聊天 如何接收带有上下文的模板并将其作为HTML附加到div中?例如。我希望无论何时通过Ajax调用GuestNameView,它都应该返回一个带有呈现的上下文的模板,我将把它作为HTML附加到project home中的div中
#encoding=utf8

from django.conf.urls.defaults import url, patterns

from . import views
from .utils.decorators import room_check_access
from .ajax import chat

urlpatterns = patterns('chatrooms',
    # room views
    url(r'^rooms/$',
    views.RoomsListView.as_view(),
    name="rooms_list"),
    url(r'^room/(?P<slug>[-\w\d]+)/$',
    room_check_access(views.RoomView.as_view()),
    name="room_view"),
    url(r'^setguestname/$',
    views.GuestNameView.as_view(),
    name="set_guestname"),

    # ajax requests
    url(r'^get_messages/', chat.ChatView().get_messages),
    url(r'^send_message/', chat.ChatView().send_message),
    url(r'^get_latest_msg_id/', chat.ChatView().get_latest_message_id),
    url(r'^get_users_list/$', chat.ChatView().get_users_list),
    url(r'^notify_users_list/$', chat.ChatView().notify_users_list),
)
#encoding=utf8

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.generic import ListView, DetailView, FormView

from .utils.auth import get_login_url
from .forms.guest import GuestNameForm
from .models import Room


class RoomsListView(ListView):
    """View to show the list of rooms available """
    context_object_name = "rooms"
    template_name = "chatrooms/rooms_list.html"
    paginate_by = 20

    def get_queryset(self):
    filters = {}
    if self.request.user.is_anonymous():
        filters['allow_anonymous_access'] = True
    return Room.objects.filter(**filters)


class RoomView(DetailView):
    """View for the single room """
    model = Room
    context_object_name = 'room'
    template_name = "chatrooms/room.html"


class GuestNameView(FormView):
    """Shows the form to choose a guest name to anonymous users """
    form_class = GuestNameForm
    template_name = 'chatrooms/guestname_form.html'

    def get_context_data(self, **kwargs):
    kwargs.update(super(GuestNameView, self).get_context_data(**kwargs))
    room_slug = self.request.GET.get('room_slug')
    next = ''
    if room_slug:
        next = reverse('room_view', kwargs={'slug': room_slug})
    kwargs['login_url'] = get_login_url(next)
    return kwargs

    def get_initial(self):
    init = super(GuestNameView, self).get_initial()
    room_slug = self.request.GET.get('room_slug')
    if room_slug:
        init.update(room_slug=room_slug)
    return init

    def form_valid(self, form):
    guest_name = form.cleaned_data.get('guest_name')
    room_slug = form.cleaned_data.get('room_slug')
    self.request.session['guest_name'] = guest_name
    if room_slug:
        redirect_url = reverse('room_view', kwargs={'slug': room_slug})
    else:
        redirect_url = reverse('rooms_list')
    return HttpResponseRedirect(redirect_url)
def home_view(request):
    site = Site.objects.get_current()
    return render_to_response('home.html', {'user':request.user,'data':request.POST , 'site':site})