How to use Django (Class)Views for a particular <div> in the template (through Ajax) ;I am integrating django-chatrooms app in my project -
i relatively new django . want integrate django-chatrooms(https://github.com/qubird/django-chatrooms) app project . app has 3 different urls (1.available-chatrooms list 2.set-guestname/login 3.room(actual chat interface)) . want make these 3 features (join room,set guestname/login , chatwindow)available user @ same url (which project homepage).so doing through ajax .
i need call 3 class views app ; corresponding url's , views files of app :
urls.py
#encoding=utf8 django.conf.urls.defaults import url, patterns . import views .utils.decorators import room_check_access .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), )
views.py
#encoding=utf8 django.core.urlresolvers import reverse django.http import httpresponseredirect django.views.generic import listview, detailview, formview .utils.auth import get_login_url .forms.guest import guestnameform .models import room class roomslistview(listview): """view show 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 single room """ model = room context_object_name = 'room' template_name = "chatrooms/room.html" class guestnameview(formview): """shows form choose guest name 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)
my home view containing home.html template (where want integrate chat)
def home_view(request): site = site.objects.get_current() return render_to_response('home.html', {'user':request.user,'data':request.post , 'site':site})
how receive template it's context , append div html ? (for eg . want whenever call guestnameview through ajax, should return me template context rendered append html div @ project home )
Comments
Post a Comment