Coderzhub code samples. Codershub is a social network for coders and aims to bring coders together on original projects. Projects are imported from Github for a link to code can be established. 

The previous post defined the models, and the way the view and controller are used in Django requires a definition of the business code to handle the data. This function checks if the user is authenticated or else it sends the user back to login and create a session cookie. Through this function, the server handles the POST request data from a form in the view file.


def CreateForum(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/login")
    if request.method == 'POST':
        forum_form = ForumForm(request.POST) if forum_form.is_valid():
        new_forum = new_forum.public = True
        return HttpResponseRedirect('/projects/forums')
        forum_form = ForumForm()
    return render(request, 'forum/add_forum.html', {'forum_form': forum_form,})


Though to map the URL to this particular method, Django needs to easily access the definition. For example, the function name is assigned to a unique URL for the server to direct traffic to.

urlpatterns = patterns('',
   url(r'^$', ProjectDashboard, name='')
   url(r'^forums/$', PublicForums, name='Public Forums'), url(r'^create/forum/$', CreateForum, name='Create Forum'),


Users are able to vote on thread to increase their popularity. The more popular the thread is the more likely users will see it. To enhance the user experience, when a user voted on a thread, it could not be refreshed as if a major submit was pressed. The submission had to be in real time. This is the advantage of Ajax to create an asynchronous web application. Since there are multiple threads, each require a unique id to send the vote to the right thread. A general Ajax function is needed to keep code small and maintainable. When the header is generated in HTML, a hidden input with the value of the thread ID is places there.


<h3 class="panel-title" id="panel-title">{{t.topic}}
 <span class="pull-right label label-info" id="votes">{{t.votes}}</span>
 <input type="hidden" value="{{}}" name="thread_id" id="thread_id" /></h3>


Next, Jquery’s ‘.on’ call is necessary to send the AJAX call when the up-vote or down- vote button is pressed multiple times on the same page. This can be done with the following JavaScript/JQuery code. The buttons are within the same <div> tag of the hidden input, so we can use multiple Jquery functions such as parent() to transverse up the DOM tree to find the hidden input, then a AJAX POST data is sent with a command to add or subtract a vote, along with the thread ID.


 function vote(data) {
 url = '/projects/vote/';
    url: url,
    type: 'post',
    data: data,
    success: function(data) {},
    failure: function(data) {}
 $(".upvote").on("click", function() {

      voting = $(this).parent().parent().find('#votes');
      id = $(this).parent().parent().find('#panel- title').children('input:hidden').val();
      votes = parseInt(voting.text()) + 1;
      data = {
        'id': id,
        'vote': 'upvote'
    return voting.text(votes);


In an attempt to divide searching into multiple functions rather than a single one is due to the fact that multiple tables would require to be queried to produce the results and kept the initial stage simple to implement. An attempt to not rely on the way the database orders the queries results, a search algorithm that first checks likeness of words such as ‘py’ to ‘python’ was developed, but misspellings made it very inefficient and error prone. The follow code shows a simple query done in a controller method.


def search_by_user(request, username):
    users = HubUser.objects.filter(user__username__contains=username)
    if users:
        user_list = []
        for user in users:
           projects = Project.objects.filter(owners=user)            
           user_list.append({'username': user.user, 'github_name': user.github, 'project_count': projects.count()})
        return render(request, 'search/search_list.html', {'user_name': username,                                                'user_list': user_list})
        return HttpResponseRedirect('/search/error')

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *

This site uses Akismet to reduce spam. Learn how your comment data is processed.