With 'config.add_view', you write the method calls yourself, so it's
clear in the code exactly which views are being activated when, and if
an exception occurs it points to that specific line.

With 'config.scan', it lists the modules internally and imports them
using a kind of hack, and you have to trust that the scanner will find
all the declarations and correctly add them, or else you have to trace
why it didn't. It also imports every module it scans, which can have
side effects if the module contains global initialization code. When
you import a module, you know exactly when it will be imported. When
scan imports a module, it just happens sometime during the scan. I
don't want to make a big deal out of it. 'config.scan' works; a lot of
people use it.

You put the calls in your main function, or in something that your
main function calls or includes. If you have a pageful of routes and a
lot of views, you might have a routes.py and a views/__init__.py . My
current application has a lot of configuration (authorization,
sessions, custom Request methods, etc), so I put the miscellaneous
configuration modules in appname/app/, so I have
appname/app//routes.py and appname/app/views.py (separate from the
appname/views/ package where the views themselves are). You can put
the views in a regular function with a 'config' argument, or put them
in an 'includeme(config)' function and use
'config.include(the_module)' -- they both do the same thing.


On Wed, Nov 11, 2015 at 12:35 PM, kk <[email protected]> wrote:
>
>
> Hi Mike,
> How is config.add_view less magical?
> And which is actually the place to write add_view?
> Do i put this decorator just above the view function just like
> view_config is put?
> Is the only difference in using or not using the scann command?
> Happy hacking.
> Krishnakant.
> On Thursday 12 November 2015 01:57 AM, Mike Orr wrote:
>>
>> On Wed, Nov 11, 2015 at 1:26 AM, Krishnakant Mane <[email protected]>
>> wrote:
>>>
>>>
>>> On Monday, November 9, 2015 at 11:24:19 PM UTC+5:30, Jonathan Vanasco
>>> wrote:
>>>>
>>>> You can have views in multiple files, and even different packages.
>>>>
>>>> `views.py` is just a "scaffold" or reference implementation.
>>>>
>>>> IIRC, Pyramid will automatically scan either `views.py` or a  `views/`
>>>> package directory and subdirectories by default.  (ie, everything with
>>>> an
>>>> `__init__.py`)
>>>>
>>>> If you want to scan other packages/directories, you can even use dotted
>>>> notation to explicitly scan them as well:
>>>>
>>>>      config.scan("myapp.views_a")
>>>
>>> So should I do config.scann for the views package?
>>> Or is it that Pyramid will automatically see my package name?
>>> I am still confused how Pyramid will automatically know which files
>>> contain
>>> my view code?
>>
>> It doesn't automatically know. There are two ways to register views:
>>
>> 1) Use 'config.add_view()' for every view.
>>
>> 2) Decorate the views with '@view_config' and then run 'config.scan()'
>> over the module or package. The scanning does the add_view's for you.
>> You can specify which package to scan, and it will scan all modules
>> and subpackages under it.
>>
>> Some people prefer one way, and some the other. I used to use
>> '@view_config' but I'm currently using 'config.add_view()' because
>> it's less magical.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.



-- 
Mike Orr <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to