0

Depending on the weekday, I want the page to show either Monday, Tuesday, Wed templates and so forth by recognizing system dates. First, Monday, Tuesday, Wed are updated via admin page and corresponding html pages are shown. Below are the snippets, but there seems to be some error, as it's returning

"didn't return an HttpResponse object. It returned None instead." 

For instance for days page,

if today is Monday, it should show monday.html
if today is Tuesday, it should show tuesday.html by recognizing system dates.

urls.py

urlpatterns = patterns('',
    url(r'^days/$', views.days_view, name='days'),)

views.py

from datetime import date
from django.shortcuts import render
def days_view(request):
    abc = datetime.datetime.today().weekday()
    if abc == '1':
        return render(request, 'app/monday.html')
    elif abc == '2':
        return render(request, 'app/tuesday.html')
    else:
        return HttpResponse('<h1>Page was not found</h1>')

models.py ( tuesday, wed... are same as monday)

class Monday(models.Model):
    author = models.ForeignKey(User, null=True, blank=True, editable=False)
    title = models.CharField(max_length=300)
    text = models.TextField()
    created_date = models.DateTimeField(
            default=timezone.now)
    published_date = models.DateTimeField(
            blank=True, null=True, default=timezone.now)

def publish(self):
    self.published_date = timezone.now()
    self.save()

def __str__(self):
    return self.title

Monday.html

  {% for monday in mondays %}
  <td><h5>{{ monday.title }}</h5></td>
  <td><h5>{{ monday.text }}</h5></td>
Jus
  • 3
  • 2
  • What does your function `days_view` return if `abc` is not Monday or Tuesday? – Shang Wang Jan 26 '16 at 19:32
  • what are you trying to do? You are not sending `mondays`, `tuesdays`, etc in context. The for loop would never get rendered. Also, `datetime.datetime.today()` does not return the day of the week. – karthikr Jan 26 '16 at 19:39

1 Answers1

2

Maybe my hint didn't help you find out what problem you have. The error message couldn't be any clearer. You days_view didn't return anything if non of the conditions met.

Edit:

I saw you edited your question. It's almost trivial to google for a solution on which day of week given a date python.

ReEdit::

To return a model object in context you need:

return render(request, 'app/monday.html', context={'monday': monday_obj})

ReReEdit::

You cannot compare an integer and a char: if abc == '1':. You need if abc == 1:.

Community
  • 1
  • 1
Shang Wang
  • 24,909
  • 20
  • 73
  • 94