Django save_model not being called on create

I just spent an hour trying to figure out why my save_model was not being called on the creation of a model but got called fine during edits. It's not like the save_model code was particularly complicated or anything like that. It was, in fact, about as simple as it got:

def save_model(self,request,model,form,change):
    if not change:
        model.created_by = request.user
    model.save()

If this doesn't work, make sure you are developing against the latest Django version. A bug was reported and fixed 5-6 months ago, so you might already have the latest version, but make sure.

Find ip address of your mac

To find your Mac's IP address, type the following command in Terminal:

ifconfig

Note that it's ifconfig, not ipconfig as you might expect. You will see a bunch of data. You can find your IP address in the en1 section on the line that starts with inet.

If you're not into Terminal, you can also find it by going System Preferences > Network > Advanced > TCP/IP. You will see your IP address listed next to "IPv4 Address."

Error when changing tables in SQL Server 2008

You might get a weird error when you try to change a table in SQL Server 2008:

"Saving changes is not permitted. The changes you have made require the following tables to be dropped or re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created."

This is followed by a list of tables that can't be changed.

You will probably get this error due to the latter (the option) than the former (the table can't be recreated). To get rid of it, go to Tools > Options > Designers > Table and Database Designers. You should see an option that says "Prevent saving changes that require table re-creation." Uncheck it, and you should be good to go.

Setting up a RemoteApp

Creating a new RemoteApp is super simple once your Terminal Server is setup to allow RemoteApps. The TS RemoteApp Manager, which allows you to manage your remote applications, can be found in Control Panel > Administrative Tools > Terminal Services.

Creating a new RemoteApp

  1. Click on the "Add RemoteApp Programs" in the "Actions" menu to the right.
  2. Click next
  3. Select your program
  4. Click next
  5. Click finish

Changing properties on the RemoteApp

The RemoteApp program should show up in the "RemoteApp Programs" list at the bottom. Right-click on the RemoteApp you just created and select "Properties." You can update the location, the alias, and command line arguments.

Creating the RDP file

To create the RDP file that will let users connect to the RemoteApp, select the RemoteApp you just created. A menu should show up in the "Actions" menu specific to your application. Click on "Create .rdp File." You can choose where to save the RDP file, terminal server settings, gateway settings, and certificate settings.

Running ASP.NET applications on Windows 7 machines

You might get a weird error when you publish your .NET site to a Windows 7 machine:

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

Config Error This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".  

The fix for this requires a change to applicationHost.config, located at:

%windir%\system32\inetsrv\config\

Change the following section overrideModeDefault values from "Deny" to "Allow" for the system.webServer section in configSections:

<section name="**handlers**" overrideModeDefault="Deny" /> 
<section name="**modules**" allowDefinition="MachineToApplication" overrideModeDefault="Deny" />

You might also get the following 404 error:

The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Module StaticFileModule
Notification ExecuteRequestHandler
Handler StaticFile
Error Code 0x80070032

You will get this error if your IIS isn't configured to run ASP.NET applications. Go to Control Panel > Programs > Turn Windows features on or off > Internet Information Services > World Wide Web Services > Application Development Features and select "ASP.NET" (which will automatically select all required dependencies).

You might then get this error:

Configuration Error 
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

You will get this error if your website is in .NET 4.0 but the app pool that your website is running under is targeting .NET 2.0. To fix it, go to IIS Manager > Application Pools and select your website's app pool (probably the DefaultAppPool). Click on "Basic Settings" in the "Actions" window to the right and select the right version in the ".NET Framework version" dropdown.

Finally, you mighet get this error:

Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

This can happen if ASP.NET 4.0 installatoin wasn't registered correctly; you can fix it by running the following command from the cmd prompt:

%windir%\Microsoft.NET\Framework\v[version number]\aspnet_regiis.exe -i

You should finally see your website.

Display DNS cache in Windows and Mac

Whenever you visit a website, your computer needs to retrieve the IP address for that address. To improve efficiency and speed, your computer will cache the name resolutions, so that if you visit Google a second time, you don't have to ask for the IP address again.

In Windows, if you want to view your DNS cache, the command is:

ipconfig /displaydns

In Mac (Snow Leopard), you can do the same thing by:

dscacheutil -cachedump -entries Host 

And you can flush your DNS cache with:

ipconfig /flushdns

in Windows and:

dscacheutil -flushcache

in Mac.

Navisphere: Flexible menu creation framework for ASP.NET MVC

I talked about Navisphere a menu-creation framework for ASP.NET MVC in a previous article. I finally got around to uploading the source.

View Navisphere on GitHub

Searching for a user in AD in .NET 3.5

.NET 3.5 brought with it the new System.DirectoryServices.AccountManagement namespace, which makes working with Active Directory so much easier. Let's take a look at how to find a user if you know their username (SAMAccountName):

private static UserPrincipal GetPrincipal(string name)
{
    var context = new PrincipalContext(
        ContextType.Domain, "yourdomain.com");

    var principal = UserPrincipal.FindByIdentity(
        context, IdentityType.SamAccountName, name);

    return principal;
}

Wow, three entire lines of code. I don't know how we'll ever convince anyone to switch from the old way of doing things. :-) How about getting the user's groups?

PrincipalSearchResult<Principal> GetGroups(UserPrincipal principal)
{
    return principal.GetAuthorizationGroups();  
}

How about you just want to dump properties about that principal?

private static IDictionary<string, string> GetProperties(UserPrincipal principal)
{
    var properties = new Dictionary<string, string>();

    var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;

    var allProperties = directoryEntry.Properties;

    foreach (var property in allProperties.PropertyNames)
    {
        var propertyName = property.ToString();

        var value = string.Empty;

        if (allProperties[propertyName] != null && allProperties[propertyName].Count > 0)
        {
            foreach (var val in allProperties[propertyName])
            {
                if (val != null)
                {
                    value += ", " + val.ToString();
                }
            }
        }

        properties.Add(property.ToString(), value); 
    }

    return properties;
}

1 of 19 pages  1 2 3 >  Last »

On the Side