CS412
Fall 2025

Quiz 3 Information

Material covered

The quiz will focus on the material that we have discussed in class, with an emphasis on material covered during this part of the course. These topics include:

Quiz details

Sample Questions

The following sample questions are provided to give you an idea about how the questions will be posed, but this is NOT a complete or sufficient study guide.

  1. Explain what is a primary-key/foreign-key relationship.

  2. Explain the difference between a using a URLField to refer to an image and an ImageField. What are the consequences for referring to the image within the img tag?

  3. Identify and explain the 2 roles and responses of the UpdateView, with respect to the GET/POST methods.

  4. Explain why it is necessary to implement the get_success_url method on a subclass of the generic DeleteView.

  5. Explain what the Django object manager is. Explain these methods on the object manager: get, filter, and exclude.

Sample Programming Questions

Given this model definition:

class Article(models.Model):
    '''Encapsulate the idea of an Article by some author.'''

    # data attributes of a Article:
    title = models.TextField(blank=False)
    author = models.TextField(blank=False)
    text = models.TextField(blank=False)
    published = models.DateTimeField(auto_now=True)
    image_file = models.ImageField()

    def __str__(self):
        '''Return a string representation of this Article object.'''
        return f'{self.title} by {self.author}'


class Comment(models.Model):
    '''Encapsulate the idea of a Comment on an Article.'''

    # data attributes of a Comment:
    article = models.ForeignKey("Article", on_delete=models.CASCADE)
    author = models.TextField(blank=False)
    text = models.TextField(blank=False)
    published = models.DateTimeField(auto_now=True)

    def __str__(self):
        '''Return a string representation of this Comment object.'''
        return f'{self.text}'

This view definition:

class ShowAllView(ListView):
    '''Create a subclass of ListView to display all blog articles.'''

    model = Article # retrieve objects of type Article from the database
    template_name = 'blog/show_all.html'
    context_object_name = 'articles' # how to find the data in the template file

And this URL pattern defintion:

urlpatterns = [
    path('', ShowAllView.as_view(), name='show_all'), 
]
  1. Write a method to get all Comments on an Article.

  2. Write the HTML source code to display the image as part of an Article.

  3. Write a subclass of UpdateView to update a Comment. Write the URL pattern for this view.

  4. Write the code to retrieve all Comments on all Articles published by 'John Lennon', where the Comments were published after 2025-06-05.