There was also a typo in my response, as I wasn't using the aliased imports
in the calls to `path`. Also, the single letters were just an illustration
(clear variable naming is key).

Also, I would not import any app's views in the maul URLconf. This kind of
defeats Django's "plug and play" application architecture. I would only
import each Django app's views in its urls.py config. file, and not in the
main project's URLconf.

In the end, the main project should know nothing about any app that may be
included, except adding that app to the "INSTALLED_APPS" list in settings
and wiring up the app's URLs in the main URLconf *without* importing its
views from there.


Kind regards,
Sithembewena


*Sent with Shift
<https://tryshift.com/?utm_source=SentWithShift&utm_campaign=Sent%20with%20Shift%20Signature&utm_medium=Email%20Signature&utm_content=General%20Email%20Group>*

On Wed, Apr 3, 2019 at 3:36 AM drone4four <drone4f...@gmail.com> wrote:

>
> Thank you Lloyd:
>
> You are right that my import statements are written inside my urls.py in
> such away that they override each other, confusing Django. I appreciate
> your advice, but with all due respect, a slightly more Pythonic approach
> would be this:
>
> “””
>
> from django.contrib import admin
>
> from django.urls import path, re_path
>
> # from . import views
>
> from posts.views import *
>
> from redactors.views import *
>
> from counters.views import *
>
> urlpatterns = [
>
>  path('admin/', admin.site.urls),
>
>  re_path('^$', home, name='home'),
>
>  re_path('^result/', result, name='result'),
>
>  re_path('^seth/', counters, name='seth'),
>
>  re_path('^james/', posts, name='james'),
>
>  re_path('^simon/', redactors, name='simon'),
>
> ]
>
> “””
>
> I find the re_path’s as they appear here to be more human readable than
> single alphabetical letters.
>
> To explain with clarity, lines 4,5,6 import all functions from within the
> app directories and within each views.py package.
>
> Also, Django 2.0 now requires regex paths in url patterns to use the
> re_path() function as describe here:
> https://docs.djangoproject.com/en/2.2/ref/urls/#re-path
>
> My Django server now runs without reporting an issue at system check.
>
> To answer your question, Joel: Yes, there is a function named posts
> inside views.py.
>
> On Monday, April 1, 2019 at 10:30:45 PM UTC-4, Lloyd Dube wrote:
>>
>> I don't know the contents of the course you are taking, but it looks like
>> the last import of views, which is "from counters import views" is
>> overriding all the others and in it you have not defined a "posts" view.
>>
>> 1. You could solve this by aliasing your imports, e.g.:
>> ```
>> from django.contrib import admin
>> from django.urls import path
>> # from . import views as a
>> from posts import views as b
>> from redactors import views as c
>> *from counters import views as d*
>> urlpatterns = [
>>   path('admin/', admin.site.urls),
>>   path('^$', a.home, name='home'),
>>   path('^b/', views.result, name='result'),
>>   path('^b/', views.counters, name='seth'),
>>   *path('^d/', views.posts, name='james'),*
>>   path('^d/', views.redactors, name='simon'),
>> ]
>> ```
>>
>> ... although this is not what you should be doing with django.
>>
>> 2. (much better) you could use the `include` function of the django.urls
>> module and pass in the string representation of each urls module's name.
>> You would not need to import each app's urls.py module for this.
>>
>> ```
>> from django.urls import *include*, path
>>
>> urlpatterns = [
>>   path('admin/', admin.site.urls),
>>   path('^james', *include*('posts.urls')),
>> ]
>> ```
>>
>> ...etc.
>>
>> I would recommend doing a short Python tutorial to understand some key
>> basics, such as how imports work (
>> https://www.w3schools.com/python/default.asp) and doing the official
>> Django "Getting Started" guide and tutorial (you can find the latest
>> version on https://www.djangoproject.com/start/).
>>
>>
>> Kind regards,
>> Sithembewena
>>
>>
>> *Sent with Shift
>> <https://tryshift.com/?utm_source=SentWithShift&utm_campaign=Sent%20with%20Shift%20Signature&utm_medium=Email%20Signature&utm_content=General%20Email%20Group>*
>>
>> On Tue, Apr 2, 2019 at 3:59 AM drone4four <drone...@gmail.com> wrote:
>>
>>> I’m taking a Udemy course by Nick Walter and rather than copying line by
>>> line, I’m trying to branch out and experiment on my own.
>>>
>>> The purpose of the website I am creating is for a small blog, with the
>>> ability to redact string input (in an HTML form) from the user. There is
>>> also a word counter for the body content of the blog.
>>>
>>> I got Django running but as soon as I started adding the code I wrote,
>>> Django stopped running properly.
>>>
>>> Here is the traceback in full: https://pastebin.com/8HtdNwPP
>>>
>>> The main issue shows at the bottom:
>>>
>>> File
>>>> "/home/<user>/dev/projects/python/2018-and-2019/CC_Redact_Iter2/CC_Redact_Iter2/urls.py",
>>>> line 28, in <module>
>>>>    * path('^james/', views.posts, name='james'),*
>>>> *AttributeError: module 'counters.views' has no attribute 'posts’*
>>>
>>>
>>>
>>> Based on this traceback, I gather I have probably misnamed a function or
>>> a file name or template but I can’t for the life of me figure which one or
>>> where.
>>>
>>> My entire source code repo can be found here:
>>> https://github.com/Angeles4four/CC_Redact_Iter2
>>>
>>> Here are some of the relevant files involved.
>>>
>>> urls.py:
>>>
>>> from django.contrib import admin
>>>> from django.urls import path
>>>> # from . import views
>>>> from posts import views
>>>> from redactors import views
>>>> from counters import views
>>>> urlpatterns = [
>>>>   path('admin/', admin.site.urls),
>>>>   path('^$', views.home, name='home'),
>>>>   path('^result/', views.result, name='result'),
>>>>   path('^seth/', views.counters, name='seth'),
>>>>   path('^james/', views.posts, name='james'),
>>>>   path('^james/', views.redactors, name='simon'),
>>>> ]
>>>
>>>
>>>
>>> counters/views.py:
>>>
>>> from django.http import HttpResponse
>>>> from django.shortcuts import render
>>>> def home(request):
>>>>   if 'ccEntry' in request.GET:
>>>>       number = request.GET['ccEntry']
>>>>       redacted_num = 'xxxx xxxx xxxx {}'.format(number[-4:])
>>>>       return render(request, 'result.html', {'number':number,
>>>> 'redacted_num':redacted_num})
>>>>   else:
>>>>       return render(request, 'home.html')
>>>> def result(request):
>>>>    return render(request, 'result.html')
>>>> def counters(request):
>>>>   return render(request, 'counters/james.html')
>>>
>>>
>>> Here is my file tree: https://imgur.com/a/BUTKKEH
>>>
>>> Contents of requirements.txt:
>>>
>>>> Django==2.0.13
>>>> Pillow==5.4.1
>>>> psycopg2==2.7.7
>>>> psycopg2-binary==2.7.7
>>>> pytz==2018.9
>>>
>>>
>>>
>>> If there are other files in my project that you wish to view, you can
>>> click through the file tree as it appears on GitHub (linked to above).
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/353c83b5-1f37-4e23-b6c7-f488cb05805e%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/353c83b5-1f37-4e23-b6c7-f488cb05805e%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a3d83e7a-2e85-41e1-99a2-7da5228b9e32%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a3d83e7a-2e85-41e1-99a2-7da5228b9e32%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAH-SnCDTmiesNCo0rT7bD8FCSYL9ueXv57S9seYBDZFYZPTQZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to