Advertisement
jules_lewis

Django view/app problem

Apr 8th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. This version works (so you can assume the models and templates work):
  2.  
  3. [first_project]
  4.  
  5.     [settings.py]
  6.  
  7.     INSTALLED_APPS = [
  8.         'django.contrib.admin',
  9.         'django.contrib.auth',
  10.         'django.contrib.contenttypes',
  11.         'django.contrib.sessions',
  12.         'django.contrib.messages',
  13.         'django.contrib.staticfiles',
  14.         'first_app',
  15.         'help',
  16.         'users',
  17.     ]
  18.  
  19.     [urls.py]
  20.  
  21.     from django.contrib import admin
  22.     from django.urls import path, include
  23.     from first_app import views
  24.  
  25.     urlpatterns = [
  26.         path('', views.index, name="index"),
  27.         path('first_app/', include('first_app.urls')),
  28.         path('users/', include('users.urls')),
  29.         path('help/', include('help.urls')),
  30.         path('admin/', admin.site.urls),
  31.     ]
  32.  
  33.  
  34. [first_app]
  35.  
  36.     [urls.py]
  37.  
  38.     from django.urls import path
  39.     from . import views
  40.  
  41.     urlpatterns = [
  42.         path('', views.index, name='index'),
  43.     ]
  44.  
  45.     [views.py]
  46.  
  47.     from django.shortcuts import render
  48.     from django.http import HttpResponse
  49.     from first_app.models import Topic, Webpage, AccessRecord
  50.  
  51.     def index(request):
  52.         webpages_list = AccessRecord.objects.order_by('date')
  53.         date_dict = {'access_records':webpages_list}
  54.         return render(request,'first_app/index.html',context=date_dict)
  55.  
  56. [help]
  57.  
  58.     [urls.py]
  59.  
  60.     from django.urls import path
  61.     from . import views
  62.  
  63.     urlpatterns = [
  64.         path('', views.help, name='help'),
  65.     ]
  66.  
  67.     [views.py]
  68.  
  69.     from django.shortcuts import render
  70.     from django.http import HttpResponse
  71.  
  72.     def help(request):
  73.         dict_vars = {'var_name':'Jules'}
  74.         return render(request,'help/help.html',context=dict_vars)
  75.  
  76. [users]
  77.  
  78.     [urls.py]
  79.  
  80.     from django.urls import path
  81.     from . import views
  82.  
  83.     urlpatterns = [
  84.         path('', views.users, name='users'),
  85.     ]
  86.  
  87.     [views.py]
  88.  
  89.     from django.shortcuts import render
  90.     from django.http import HttpResponse
  91.  
  92.     def users(request):
  93.         dict_vars = {'var_name':'Jules'}
  94.         return render(request,'users/users.html',context=dict_vars)
  95.  
  96.  
  97.  
  98. This version does not work.
  99.  - The users app is removed and view moved to first_app
  100.  - The users.html template is moved to the correct template folder
  101.  - the help app does not change; it is just here for completeness
  102.  
  103. [first_project]
  104.  
  105.     [settings.py]
  106.  
  107.     INSTALLED_APPS = [
  108.         'django.contrib.admin',
  109.         'django.contrib.auth',
  110.         'django.contrib.contenttypes',
  111.         'django.contrib.sessions',
  112.         'django.contrib.messages',
  113.         'django.contrib.staticfiles',
  114.         'first_app',
  115.         'help',
  116.     ]
  117.  
  118.     [urls.py]
  119.  
  120.     from django.contrib import admin
  121.     from django.urls import path, include
  122.     from first_app import views
  123.  
  124.     urlpatterns = [
  125.         path('', views.index, name="index"),
  126.         path('first_app/', include('first_app.urls')),
  127.         path('users/', include('first_app.urls')),      <---- this line changes
  128.         path('help/', include('help.urls')),
  129.         path('admin/', admin.site.urls),
  130.     ]
  131.  
  132.  
  133. [first_app]
  134.  
  135.     [urls.py]
  136.  
  137.     from django.urls import path
  138.     from . import views
  139.  
  140.     urlpatterns = [
  141.         path('', views.index, name='index'),
  142.         path('users/', views.users, name='users'),
  143.     ]
  144.  
  145.     [views.py]
  146.  
  147.     from django.shortcuts import render
  148.     from django.http import HttpResponse
  149.     from first_app.models import Topic, Webpage, AccessRecord
  150.  
  151.     def index(request):
  152.         webpages_list = AccessRecord.objects.order_by('date')
  153.         date_dict = {'access_records':webpages_list}
  154.         return render(request,'first_app/index.html',context=date_dict)
  155.  
  156.     def users(request):
  157.         dict_vars = {'var_name':'Jules'}
  158.         return render(request,'first_app/users.html',context=dict_vars)
  159.  
  160. [help]
  161.  
  162.     [urls.py]
  163.  
  164.     from django.urls import path
  165.     from . import views
  166.  
  167.     urlpatterns = [
  168.         path('', views.help, name='help'),
  169.     ]
  170.  
  171.     [views.py]
  172.  
  173.     from django.shortcuts import render
  174.     from django.http import HttpResponse
  175.  
  176.     def help(request):
  177.         dict_vars = {'var_name':'Jules'}
  178.         return render(request,'help/help.html',context=dict_vars)
  179.  
  180.  
  181. In the first version, when I navigate to http://127.0.0.1:8000/users I see users.html, as expected.
  182. In the second version, when I navigate to http://127.0.0.1:8000/users I see index.html from first_app!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement