Python | Django URLs

Last Updated :
Discuss
Comments

Question 1

Where are URL patterns in Django stored so that the framework knows how to route requests?

  • In views.py only

  • In a module defined by ROOT_URLCONF, typically urls.py

  • Hard-coded into each template

  • In settings.py under a list called urlpatterns

Question 2

What is the effect of using path('books/<int:pk>/', views.book_detail, name='book_detail') in a urls.py file?

  • It captures an integer value from the URL and passes it as pk to views.book_detail

  • It matches only URLs exactly named “books”

  • It creates a database table for books

  • It disables routing for “books” URLs

Question 3

What is the benefit of giving URL patterns a name= parameter in Django?

  • It automatically generates HTML for the view

  • It allows reverse URL lookup in templates and code without hard-coding the path

  • It prevents users from accessing the URL directly

  • It defines the HTTP method allowed for the view

Question 4

Which Django function should be used if a complex regular-expression-based URL pattern is required instead of the simpler path()?

  • url()

  • complex_path()

  • re_path()

  • fancy_path()

Question 5

When a request arrives, how does Django’s URL dispatcher decide which view to call?

  • It randomly picks a view

  • It uses the view name to search for a pattern

  • It requires the client to specify the view explicitly

  • It checks URL patterns in the order defined and calls the view tied to the first match

Question 6

What does URLField() do to input like 'example.com'?

  • Accepts it always.

  • Shortens it automatically.

  • Converts to slug.

  • Checks for http:// or https://, errors otherwise.

Question 7

In a URL shortener, why use a short slug like 'abc123' for long URLs?

  • For easy sharing, like tinyurl.com/abc123.

  • To save database space only.

  • To encrypt the original URL.

  • To add images.

Question 8

In a view, how to make a random 6-char slug with letters and numbers?

  • Use hash(original_url)[:6].

  • datetime.now() string.

  • len(original_url) for length.

  • ''.join(random.choices('abc123def456', k=6)).

Question 9

If reverse('missing') errors with NoReverseMatch, what's likely wrong?

  • View function not defined.

  • Database not migrated.

  • No name='missing' in any path.

  • Server not running.

Question 10

An ImproperlyConfigured from ROOT_URLCONF points to wrong module; what fix in settings.py, and why restart server after?

  • Set ROOT_URLCONF = 'project.urls'; restart clears cache for new patterns.


  • Set ROOT_URLCONF = 'app.urls'; no restart, as configs load dynamically.

  • Add to INSTALLED_APPS; restart for view imports only.

  • Run makemigrations; restart for DB sync.

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion